The pthread_mutex_destroy() API is used to destroy the mutex object referenced by mutex.
Prototype:
int pthread_mutex_destroy (pthread_mutex_t *mutex);
- The pthread_mutex_destroy() function shall destroy the mutex object referenced by mutex; the mutex object becomes, in effect, uninitialized.
- The mutex must not be used after it has been destroyed.
- Mutexes are used to protect shared resources. mutex is set to an invalid value, but can be reinitialized using pthread_mutex_init().
Return Values:
On success it returns 0 and on failure, an error code is returned.
Sample program:
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
int main()
{
pthread_mutex_t mutex;
int ret;
ret = pthread_mutex_init (&mutex, NULL);
if (ret)
{
printf ("\n Mutex initialization failed %s\n", strerror (errno));
exit (EXIT_FAILURE);
}
printf ("\n Mutex initialization success\n");
pthread_mutex_destroy (&mutex);
printf ("\n Mutex destroyed success\n");
return 0;
}
Output:
gcc mutex_destroy.c -o mutex_destroy
./mutex_destroy
Mutex initialization success
Mutex destroyed success
...Program finished with exit code 0
Press ENTER to exit console.
Categories: Operating system (OS)
Leave a Reply