Reputation: 21625
I want my threads to stop at a barrier point, but I want to only stop if a barrier is required. For example after a certain timer interval, I want all of threads to form a barrier. Is there a method to put a potential barrier point? For example at start of each function. I want something like this.
void some_function()
{
potentially_barrier_wait;
......
}
So I want potentially_barrier_wait
to only call a barrier if its required, otherwise potentially_barrier_wait
do nothing. Can this thing be implemented satisfactorily? Will this also work in programs which already have barriers in them, or will it race with them.
Upvotes: 1
Views: 141
Reputation: 5469
Depends on what you mean by barrier... if you are referring to simple synchronization, you can do it like this:
On Windows, create an event CreateEvent( 0, false, 0, 0 ). In your thread, do a WaitForSingleObject on that. In your "controlling" thread, SetEvent it to let it pass your barrier. You can play with it to have it pre-set, and auto-release, etc.
On Linux, similar but you can use a pthread_cond_timedwait as your barrier, and signal it with pthread_cond_signal.
Upvotes: 1