#include <pthread.h>
int pthread_mutex_unlock(pthread_mutex_t *mutex);
The pthread_mutex_unlock() function attempts to unlock the specified mutex. If there are threads blocked on the mutex object when pthread_mutex_unlock() is called, resulting in the mutex becoming available, the scheduling policy is used to determine which thread acquires the mutex.
If the mutex type is PTHREAD_MUTEX_NORMAL, error detection is not provided. If a thread attempts to unlock a mutex that is has not locked or a mutex which is unlocked, undefined behavior results.
If the mutex type is PTHREAD_MUTEX_ERRORCHECK, error checking is provided. If a thread attempts to unlock a mutex that it has not locked or a mutex that is unlocked, an error is returned.
If the mutex type is PTHREAD_MUTEX_RECURSIVE, the mutex maintains the concept of a lock count. When a thread successfully acquires a mutex for the first time, the lock count is set to one. Every time a thread relocks this mutex, the lock count is incremented by one. When the lock count reaches zero, the mutex becomes available for other threads to acquire. If a thread attempts to unlock a mutex that it has not locked or a mutex that is unlocked, an error is returned.
If the mutex type is PTHREAD_MUTEX_DEFAULT, attempting to unlock the mutex if it was not locked by the calling thread results in undefined behavior. Attempting to unlock the mutex if it is not locked results in undefined behavior.
静态定义:
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
The PTHREAD_MUTEX_INITIALIZER macro initializes the static mutex mutex, setting its attributes to default values. This macro should only be used for static mutexes, as no error checking is performed.
动态定义:
pthread_mutex_t mutex;
pthread_mutex_init (&mutex, NULL);
动态定义和静态定义之间的联系:
The pthread_mutex_init() function shall initialize the mutex referenced by mutex with attributes specified by attr. If attr is NULL, the default mutex attributes are used; the effect shall be the same as passing the address of a default mutex attributes object. Upon successful initialization, the state of the mutex becomes initialized and unlocked.
In cases where default mutex attributes are appropriate, the macro PTHREAD_MUTEX_INITIALIZER can be used to initialize mutexes that are statically allocated. The effect shall be equivalent to dynamic initialization by a call to pthread_mutex_init() with parameter attr specified as NULL, except that no error checks are performed.
Reference:
http://www.duangw.net/computer/history/pthread/mutex.html
http://www.ibm.com/developerworks/cn/linux/l-pthred/
http://www.ibm.com/developerworks/cn/linux/sdk/rt/part5/
http://www.ibm.com/developerworks/cn/linux/l-osmig1.html
分享到:
相关推荐
3. `pthread_mutex_unlock()`: 在完成对共享资源的访问后,线程应调用此函数解锁互斥锁,让其他等待的线程有机会获取锁。 4. `pthread_mutex_destroy()`: 当不再需要互斥锁时,使用此函数来销毁它,释放相关资源。 ...
动态创建则通过 `pthread_cond_init()` 函数完成,但通常不提供属性(cond_attr),因为它在 LinuxThreads 中未被实现: ```c pthread_cond_init(&cond, NULL); ``` 销毁条件变量使用 `pthread_cond_destroy()`,但...
6. **线程1或其他线程调用`pthread_mutex_unlock`解锁互斥锁**。 #### 五、注意事项 1. **必须与互斥锁一起使用**:`pthread_cond_wait` 必须在一个已经由调用线程锁定的互斥锁上使用,否则会导致未定义行为。 2. ...
具体来说,`pthread_mutex_t`类型表示一个互斥锁对象,可以通过`pthread_mutex_init()`初始化,`pthread_mutex_lock()`锁定,`pthread_mutex_unlock()`解锁。在消费者和生产者之间,通常会有一个共享资源(如缓冲区...
当一个线程调用这个函数时,如果锁未被其他线程持有,它将成功获取锁并立即返回。否则,线程将被阻塞,直到锁被释放。 3. `pthread_mutex_unlock()`: 释放互斥锁。持有锁的线程调用此函数后,锁将被释放,等待中的...
如果条件不满足,线程会调用`pthread_cond_wait()`进入等待状态,并释放互斥量,让其他线程有机会获得锁并修改条件。 3. **加while循环的原因**:在`pthread_cond_wait()`前添加while循环的原因在于,即使线程被...
如果锁已经被其他线程持有,调用线程将被阻塞,直到锁被释放。 ```c pthread_mutex_lock(&mutex); // 临界区,只有获得锁的线程能到达这里 pthread_mutex_unlock(&mutex); ``` 3. `pthread_mutex_unlock()`: 释放...
在需要保护的代码段前,调用`pthread_mutex_lock()`函数上锁,防止其他线程访问共享资源: ```cpp int pthread_mutex_lock(pthread_mutex_t *mutex); ``` 4. **解锁:** 完成对共享资源的操作后,调用`pthread...
linux 内核 mutex.c 源码分析实例,我的实例主要先初始化了一个互斥体m,然后获取互斥体的锁(解锁),紧接着释放互斥体的锁(解锁)。最后释放互斥体。中间通过输出m.count来显示互斥体的状态。
1. 互斥锁:通过`pthread_mutex_init()`、`pthread_mutex_lock()`和`pthread_mutex_unlock()`来初始化、锁定和解锁互斥锁,确保同一时间只有一个线程能访问临界区。 2. 条件变量:允许线程在满足特定条件时挂起,当...
当一个线程获得锁后,其他试图获取该锁的线程将被阻塞,直到锁被释放。这样就保证了对共享资源的独占访问,避免了数据竞争问题。在Linux中,互斥锁通常通过pthread_mutex_t类型实现,而在Windows中,我们可以使用...
如果互斥量当前未被锁定,调用线程将获得锁,并返回0。如果锁已被其他线程持有,那么调用线程将被阻塞,直到锁被释放。 ```c pthread_mutex_lock(&mutex); ``` 3. **pthread_mutex_unlock()**:释放互斥量。只有...
4. 解锁:`pthread_mutex_unlock()`解锁Mutex,唤醒等待的线程。 5. 销毁:`pthread_mutex_destroy()`销毁Mutex。 Mutex有三种类型:“fast”,“recursive”和“error checking”。快速Mutex(fast)在自锁时挂起...
而`pthread_mutex_unlock()`则用于解锁互斥量,释放对临界区的控制,让其他等待的线程有机会获得锁。 在实际应用中,通常会在进入临界区之前调用`pthread_mutex_lock()`,退出临界区后调用`pthread_mutex_unlock()`...
3. 互斥锁解锁:pthread_mutex_unlock 4. 消除互斥锁:pthread_mutex_destroy 互斥锁的相关函数包括: * pthread_mutex_init:初始化锁 * pthread_mutex_lock:访问资源 * pthread_mutex_unlock:释放资源 * ...
- **互斥量**(Mutex):`pthread_mutex_t`是互斥量类型,`pthread_mutex_init()`和`pthread_mutex_destroy()`用于初始化和销毁互斥量,`pthread_mutex_lock()`和`pthread_mutex_unlock()`用于锁定和解锁,防止多个...
当一个线程获得了互斥锁,其他尝试获取该锁的线程将被阻塞,直到持有锁的线程释放它。这样,可以确保同一时间只有一个线程能访问特定的代码段或数据结构,避免了竞态条件的发生。 在日志写入的场景中,互斥锁尤其...
- `pthread_mutex_trylock`:尝试锁定互斥量,但与`pthread_mutex_lock`不同,若互斥量已被其他线程锁定,则直接返回而不阻塞线程。 - `pthread_mutex_unlock`:用于解锁互斥量。 在使用互斥量时,一般流程如下: 1...
unlock主动解锁函数,同时将阻塞在该锁上的所有线程全部唤醒,至于哪个线程先被唤醒,取决于优先级、调度。默认:先阻塞、先唤醒。 例如:T1 T2 T3 T4 使用一把mutex锁。T1加锁成功,其他线程均阻塞,直至T1解锁。...
通过`pthread_mutex_lock()`和`pthread_mutex_unlock()`函数,我们可以锁定和解锁互斥锁。 2. **条件变量(pthread_cond_wait, pthread_cond_signal)** 条件变量是另一种重要的线程同步工具,允许线程在满足特定...