How do I use latch and barrier (C++20)?

C++20 introduces synchronization primitives like latch and barrier that facilitate easier thread coordination and synchronization in concurrent programming.
C++20, latch, barrier, multi-threading, synchronization
        #include <iostream>
        #include <thread>
        #include <latch>
        #include <barrier>

        void worker(int id, std::latch &l, std::barrier &b) {
            std::cout << "Worker " << id << " is doing work...\n";
            std::this_thread::sleep_for(std::chrono::seconds(1)); // Simulate work
            l.count_down(); // Signal that this worker is done
            b.arrive_and_wait(); // Wait for all workers at the barrier
            std::cout << "Worker " << id << " has crossed the barrier.\n";
        }

        int main() {
            const int num_workers = 3;
            std::latch latch(num_workers); // Initialize latch with 3
            std::barrier b(num_workers); // Initialize barrier with 3
            
            std::thread workers[num_workers];
            for (int i = 0; i < num_workers; ++i) {
                workers[i] = std::thread(worker, i, std::ref(latch), std::ref(b));
            }
            
            latch.wait(); // Wait for all workers to finish their work
            std::cout << "All workers are done with their work.\n";
            for (auto &w : workers) {
                w.join(); // Join all worker threads
            }
        
            return 0;
        }
        

C++20 latch barrier multi-threading synchronization