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.
Advertisement
Answer
Off the top of my head –
- Use a semaphore (or a shared memory counter, for that matter)
- 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.
- Create the mutex externally, and delete it externally – processes take it for granted (maybe never even delete it; depends on the system)
etc…