Tag Archives: pthread

Different Semantics Between Win32 Events and Condition Variables

Following the last post, I’m trying to implement a thread pool for practise, which supposed to work under both Windows and Linux platform. But the different semantics between Win32 events and condition variables makes it impossible to code in a unified logic. First, Linux uses mutex and condition variable to keep synchronization. While there is only event under Windows. Then, pthread_cond_signal() does nothing if no thread is currently waiting on the condition:

But under Windows, code below simply pass through:

And, under Windows Vista and later versions, a new series of synchronization API was introduced to align with the Linux API:

Sharing Mutex and Condition Variable Between Processes

As title, the key is to set an attribute(PTHREAD_PROCESS_SHARED) to the mutex/condition variable using pthread_mutexattr_setpshared() or pthread_condattr_setpshared(). Without these function calls, the parent in the following code will not get signaled forever.

Shared memory is used to share the mutex and condition variable.

NOTE: The process-shared mutex attribute isn’t universally supported yet. You should confirm before using them.