pthread_mutex_destroy()--Destroy Mutex


  Syntax:
 #include <pthread.h>
 int pthread_mutex_destroy(pthread_mutex_t *mutex);  
  Service Program Name: QP0WPTHR

  Default Public Authority: *USE

  Threadsafe: Yes

  Signal Safe: Yes

The pthread_mutex_destroy() function destroys the named mutex. The destroyed mutex can no longer be used.

If pthread_mutex_destroy() is called on a mutex that is locked by another thread, the request fails with an EBUSY error. If the calling thread has the mutex locked, any other threads waiting for the mutex using a call to pthread_mutex_lock() at the time of the call to pthread_mutex_destroy() fails with the EDESTROYED error.

Mutex initialization using the PTHREAD_MUTEX_INITIALIZER does not immediately initialize the mutex. Instead, on first use, pthread_mutex_lock() or pthread_mutex_trylock() branches into a slow path and causes the initialization of the mutex. Because a mutex is not just a simple memory object and requires that some resources be allocated by the system, an attempt to call pthread_mutex_destroy() or pthread_mutex_unlock() on a mutex that has statically initialized using PTHREAD_MUTEX_INITIALIZER and was not yet locked causes an EINVAL error.

Every mutex must eventually be destroyed with pthread_mutex_destroy(). The machine eventually detects the error if a mutex is not destroyed, but the storage is deallocated or corrupted. The machine then creates LIC log synchronization entries that indicate the failure to help debug the problem. Large numbers of these entries can affect system performance and hinder debug capabilities for other system problems. Always use pthread_mutex_destroy() before freeing mutex storage to prevent these debug LIC log entries.

Note: Once a mutex is created, it cannot be validly copied or moved to a new location.


Authorities and Locks

None.


Parameters

mutex
(Input) Address of the mutex to be destroyed

Return Value

0
pthread_mutex_destroy() was successful.
value
pthread_mutex_destroy() was not successful. value is set to indicate the error condition.

Error Conditions

If pthread_mutex_destroy() was not successful, the error condition returned usually indicates one of the following errors. Under some conditions, the value returned could indicate an error other than those listed here.

[EBUSY]

The mutex is currently owned by another thread.

[EINVAL]

The value specified for the argument is not correct.


Related Information


Example

Note: By using the code examples, you agree to the terms of the Code license and disclaimer information.

#include <pthread.h>
#include <stdio.h>
#include  "check.h"

pthread_mutex_t            mutex;

int main(int argc, char **argv)
{
  int                   rc=0;
  pthread_mutexattr_t   mta;

  printf("Entering testcase\n");

  printf("Create the mutex using the NULL attributes (default)\n");
  rc = pthread_mutex_init(&mutex, NULL);
  checkResults("pthread_mutex_init(NULL)\n", rc);
 
  printf("Destroy all mutexes\n");

  pthread_mutex_destroy(&mutex);
  checkResults("pthread_mutex_destroy()\n", rc);
 
  printf("Main completed\n");
  return 0;
}

Output:

Entering testcase
Create the mutex using the NULL attributes (default)
Destroy all mutexes
Main completed

API introduced: V4R3

[ Back to top | Pthread APIs | APIs by category ]