int pthread_mutex_destroy(pthread_mutex_t *mutex);
时间: 2024-01-26 12:14:25 浏览: 43
`int pthread_mutex_destroy(pthread_mutex_t *mutex);`函数用于销毁互斥量。它接受一个指向互斥量的指针作为参数,并将该互斥量销毁。销毁互斥量后,不能再对其进行任何操作。
以下是一个简单的示例演示如何使用`pthread_mutex_destroy`函数:
```c
#include <pthread.h>
#include <stdio.h>
pthread_mutex_t mutex;
void* thread_function(void* arg) {
pthread_mutex_lock(&mutex);
printf("Thread locked the mutex\n");
pthread_mutex_unlock(&mutex);
printf("Thread unlocked the mutex\n");
return NULL;
}
int main() {
pthread_mutex_init(&mutex, NULL);
pthread_t thread;
pthread_create(&thread, NULL, thread_function, NULL);
pthread_join(thread, NULL);
pthread_mutex_destroy(&mutex);
return 0;
}
```
在上面的示例中,我们首先使用`pthread_mutex_init`函数初始化了一个互斥量。然后,我们创建了一个线程,在线程函数中使用`pthread_mutex_lock`函数锁定了互斥量,并在之后使用`pthread_mutex_unlock`函数解锁了互斥量。最后,我们在主线程中使用`pthread_mutex_destroy`函数销毁了互斥量。