I am looking for a way to synchronize several multi-threaded processes on Linux. C++11-style mutexes only operate among threads within a single process, ans as far as I know, flock doesn't mutually excludes threads of the same process.
Therefore, I was thinking of POSIX semaphores. I know that unnamed semaphores are either thread- or process-related, but not both. What about named semaphores? Obviously they synchronize processes, but what about threads? I could not find the clear definitive answer in the documentation.
Best How To :
Yes, they synchronize threads.
The Open Group Base Specification Issue 7 clarifies under General Concepts that all semaphores synchronize threads, whether they are POSIX-style (
<semaphore.h>) or SysV-style (
[T]the semaphore lock operation shall cause the calling thread to be blocked and added to the set of threads awaiting the semaphore
Regarding your remark that "unnamed semaphores are either thread- or process-related, but not both", I don't quite know what you mean. Unnamed semaphores may be shared between (parent-child) processes if allocated in shared memory and explicitly initialized as "pshared". However, process-shared or not, unnamed semaphores synchronize individual threads.
(For what it's worth, POSIX mutexes may also be shared between processes.)