Re: about destroying pthread mutex

On Jul 17, 3:02 am, David Schwartz <dav...@xxxxxxxxxxxxx> wrote:
On Jul 16, 8:21 pm, lmike <lmike3...@xxxxxxxxx> wrote:

First of all, if I don't destroy the mutex when the process goes down,
the mutex resource won't be released by OS, correct?

No, it won't. But it probably won't exist anymore either. On most
operating systems, a mutex is just a chunk of virtual memory in the
process' address space. Since the process' address space no longer
exists, neither does the mutex.

How do you guys destroy this mutex during normal process exit? -- do
it with atexit()?

After you have cleanly shutdown all the threads that could be using
the mutex, call pthread_mutex_destroy.

Do you destroy the mutex during abnormal shutdown? -- e.g. when
process goes down because of a signal, and try to destroy the mutex in
the signal handler? Can I release the mutex resource after the process
is down (something like running ipcrm on IPCs)?

Generally, you don't try to clean up on abnormal shutdown.


Thanks, DS. That helps.