Skip to content

pthread_mutex_t in multiple processes…who destroys it?

I’m using a pthread_mutex_t to synchronize access to a resource between multiple processes which are on the same level (i.e. there isn’t a parent/child relationship).

How can I determine when it’s safe to call pthread_mutex_destroy? The only way I can think of to determine if the mutex is in use by another process without introducing a race condition is to introduce another mutex.



Off the top of my head –

  1. Use a semaphore (or a shared memory counter, for that matter)
  2. Decide that the leader (the first process to create the mutex) is the one that will destroy it, and don’t exit this process until the rest of them shut down.
  3. Create the mutex externally, and delete it externally – processes take it for granted (maybe never even delete it; depends on the system)


User contributions licensed under: CC BY-SA
8 People found this is helpful