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.

Updated Oct 14, 2020: Fixed pthread_mutexattr_t and pthread_condattr_t initialization.

1 comment

  1. Mainly the code is correct, but mattr (pthread_mutexattr_t) and cattr (pthread_condattr_t ) variables should be initialized via pthread_mutexattr_init and pthread_condattr_init functions.

Leave a Reply

Your email address will not be published. Required fields are marked *