pthread_mutex_destroy() — Delete a mutex object

Standards

Standards / Extensions C or C++ Dependencies

POSIX.4a
Single UNIX Specification, Version 3

both

POSIX(ON)

Format

#define _OPEN_THREADS
#include <pthread.h>

int pthread_mutex_destroy(pthread_mutex_t *mutex);
SUSV3:
#define _UNIX03_THREADS
#include <pthread.h>

int pthread_mutex_destroy(pthread_mutex_t *mutex);

General description

Deletes a mutex object, which identifies a mutex. Mutexes are used to protect shared resources. mutex is set to an invalid value, but can be reinitialized using pthread_mutex_init().

Returned value

If successful, pthread_mutex_destroy() returns 0.

If unsuccessful, pthread_mutex_destroy() returns -1 and sets errno to one of the following values:
Error Code
Description
EBUSY
A request has detected an attempt to destroy the object referenced by mutex while it was locked or referenced by another thread (for example, while being used in a pthread_cond_wait() or pthread_cond_timedwait() function).
EINVAL
The value specified by mutex is not valid.

Special behavior for Single UNIX Specification, Version 3: If unsuccessful, pthread_mutex_destroy() returns an error number to indicate the error.

Example

CELEBP36
⁄* CELEBP36 *⁄                                   
#define _OPEN_THREADS                                                           
#include <pthread.h>                                                            
#include <stdio.h>                                                              
                                                                                
main() {                                                                        
  pthread_mutex_t mutex;                                                        
                                                                                
  if (pthread_mutex_init(&mutex, NULL) != 0) {                                  
    perror("pthread_mutex_init() error");                                       
    exit(1);                                                                    
  }                                                                             
                                                                                
  if (pthread_mutex_destroy(&mutex) != 0) {                                     
    perror("pthread_mutex_destroy() error");                                    
    exit(2);                                                                    
  }                                                                             
}                                                                               

Related information