Skip to content

Process-Shared pthread Synchronization

pthread synchronization objects can be shared between processes when they are placed in shared memory and configured with process-shared attributes.

Mutex Attribute Flow

pthread_mutexattr_t attr;

pthread_mutexattr_init(&attr);
pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
pthread_mutex_init(&shared->lock, &attr);
pthread_mutexattr_destroy(&attr);

Key APIs

  • pthread_mutexattr_init()
  • pthread_mutexattr_setpshared()
  • pthread_mutex_init()
  • pthread_mutex_lock()
  • pthread_mutex_unlock()
  • pthread_mutex_destroy()

Common Pitfalls

Warning

The mutex object itself must live in shared memory. Setting PTHREAD_PROCESS_SHARED is not enough if the object is stored in normal process-private memory.

Note

A process-shared mutex protects shared data structure consistency. It does not provide event notification by itself.