In a past question, I asked about implementing pthread barriers without destruction races:
How can barriers be destroyable as soon as pthread_barrier_wait returns?
and received from Michael Burr with a perfect solution for process-local barriers, but which fails for process-shared barriers. We later worked through some ideas, but never reached a satisfactory conclusion, and didn't even begin to get into resource failure cases.
Is it possible on Linux to make a barrier that meets these conditions:
- Process-shared (can be created in any shared memory).
- Safe to unmap or destroy the barrier from any thread immediately after the barrier wait function returns.
- Cannot fail due to resource allocation failure.
Michael's attempt at solving the process-shared case (see the linked question) has the unfortunate property that some kind of system resource must be allocated at wait time, meaning the wait can fail. And it's unclear what a caller could reasonably do when a barrier wait fails, since the whole point of the barrier is that it's unsafe to proceed until the remaining N-1
threads have reached it...
A kernel-space solution might be the only way, but even that's difficult due to the possibility of a signal interrupting the wait with no reliable way to resume it...
pthread_barrier_destroy
function only synchronizes with the last waiter. what's the issue, exactly? – Sandlinpthread_barrier_destroy
takes, assuring that nobody accesses the barrier internals when the destroy succeeds (which it won't do if any thread is waiting). – Sandlinpthread_barrier_destroy
. In the latter case, you can later re-initialize the barrier for reuse. – SandlinPTHREAD_BARRIER_SERIAL_THREAD
) destroys and then unlinks. – Sandlin