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.

DS

Thanks, DS. That helps.
.



Relevant Pages

  • Re: about destroying pthread mutex
    ... the mutex resource won't be released by OS, ... After you have cleanly shutdown all the threads that could be using ... the signal handler? ...
    (comp.os.linux.development.apps)
  • Re: read-write mutex
    ... if signal handler thread catches SIGTERM it sets the variable to 1. ... If another thread reads old value (zero) it's ok, ... The question isn't whether you need a mutex or not, ...
    (comp.programming.threads)
  • Re: A new idea for garbage collection
    ... In my opinion, this question is quite specifically about threaded programming *in C*, because multithreaded programs in C++ or Java or any other modern language will deal with this with exceptions. ... in order before releasing a mutex to let others come in and look. ... It could be running in a signal handler, ... It is protecting something, something that was being ...
    (comp.lang.c)
  • Re: shared memory locking
    ... I read that using mutex in a signal handler isn't a good way to ... mutex_lock/unlock to update my shared memory (which is a list of ... The difference is that file record locking is safe to use from a signal ...
    (comp.unix.programmer)
  • Re: Communication between threads
    ... message queue. ... These functions block until a signal arrives ... signal handler must avoid, if you so choose. ... to lock the mutex, and blocks because the mutex is already locked. ...
    (comp.unix.programmer)