Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why destroy pthread_cond_t and pthread_mutex_t?

If in a threaded code, I create a pthread_cond_t c; condition variable or a mutex pthread_mutex_t m; in C, it is advised to destroy them after all the work is done.

Why is it so?

Also why is it utmost necessary to destroy a cond variable if it was dynamically initialized using pthread_cond_init(); function.

To quote from David Butenhof "Programming with POSIX Threads"

"When you dynamically initialize a condition variable, you should destroy the condition variable when you no longer need it, by calling pthread_cond_destroy.You do not need to destroy a condition variable that was statically initialized using the PTHREAD_COND_INITIALIZER macro"

like image 281
Suvarna Pattayil Avatar asked Apr 10 '13 11:04

Suvarna Pattayil


People also ask

How do you destroy a condition variable?

DESCRIPTION. The pthread_cond_destroy() function shall destroy the given condition variable specified by cond; the object becomes, in effect, uninitialized. An implementation may cause pthread_cond_destroy() to set the object referenced by cond to an invalid value.

Does Pthread_cond_wait unlock mutex?

The pthread_cond_wait() function atomically unlocks mutex and performs the wait for the condition. In this case, atomically means with respect to the mutex and the condition variable and another threads access to those objects through the pthread condition variable interfaces.

What is pthread_cond_init?

DESCRIPTION. The function pthread_cond_init() initialises the condition variable referenced by cond with attributes referenced by attr. If attr is NULL, the default condition variable attributes are used; the effect is the same as passing the address of a default condition variable attributes object.

What does Pthread cond broadcast do?

The pthread_cond_signal() function wakes up at least one thread that is currently waiting on the condition variable specified by cond. If no threads are currently blocked on the condition variable, this call has no effect.


1 Answers

pthread_cond_t and pthread_mutex_t are regarded as resources.

You need to destroy/cleanup/close resources when you are done with them, much like you need to close a file or free memory when you're done with them. Failure to do so results in a resource leak, and you may run out of theses resources.

Treating these as a resources gives the implementation more freedom on how to implement them, and on some particular implementation, there may be no harm in forgetting to _destroy() them, others may connect the mutex/condition variable to a kernel resource that needs to be cleaned up when you don't need it anymore. The rationale section of pthread_mutex_init gives some more overview, and the same applies to condition variables

If you initialize a condition variable with PTHREAD_COND_INITIALIZER, you're supposed to initialize a statically allocated mutex i.e. it is going to live until the application ends, at which point it will be destroyed by the system, presumably that's what the author meant. That applies to mutex/cond variables that is dynamically initialized as well, the system will clean those up as well.

Most resources are automatically cleaned up when an application ends, so whether it's good practice to clean up everything manually or just let the system do it in such case is another discussion.

like image 109
nos Avatar answered Oct 06 '22 20:10

nos