pthread_mutex_init
时间: 2023-09-13 22:03:32 浏览: 95
pthread_mutex
pthread_mutex_init() is a function in the pthreads library in C programming language that is used to initialize a mutex (short for mutual exclusion). A mutex is a variable that is used to protect shared resources in a multi-threaded environment, allowing only one thread to access the resource at a time.
The syntax for pthread_mutex_init() is:
```
int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr);
```
Where:
- `mutex` is a pointer to the mutex variable to be initialized.
- `attr` is a pointer to a mutex attribute object that specifies the mutex's properties. If this is NULL, the mutex is initialized with default attributes.
The function returns 0 on success and an error code on failure.
Once initialized, the mutex can be used with other pthreads functions to provide mutual exclusion and protect shared resources in a multi-threaded environment.
阅读全文