Does Zig (either the language or the standard library) have features for thread synchronization/coordination? In C, I can simply use pthread barriers (pthread_barrier_t
) to make each thread wait until all threads have reached the barrier. The main thread initializes the barrier with pthread_barrier_init()
, then each thread only needs to call pthread_barrier_wait()
. However, I can’t find any such functionality in the Zig standard library, so I’m wondering if I will have to implement it from scratch.
If I do implement thread barriers on my own, I assume I will have to use std.Thread.Mutex
and std.Thread.Condition
. A variable, protected by the Mutex, keeps track of the number of threads that are waiting at the barrier. Each time a thread enters the barrier, it increments that variable by 1. If the number of threads at the barrier is not equal to the total number of threads, the thread waits for the Condition. Otherwise, it sets the number of threads waiting at the barrier to zero (reset) and broadcasts the Condition, allowing all threads to continue.
Please correct me if that draft of an implementation is flawed or if you know of a much better way.
(BTW, I feel like there should be a concurrency
, parallelism
or multithreading
tag. I can’t figure out how to add a new tag, so I assume that I’m not allowed to do that.)