关于 linux 下 pthread_cond_wait 的原子操作疑问??
关于 linux 下 pthread_cond_wait 的原子操作疑问??
int pthread_cond_timedwait(pthread_cond_t *restrict cond,
pthread_mutex_t *restrict mutex,
const struct timespec *restrict abstime);
int pthread_cond_wait(pthread_cond_t *restrict cond,
pthread_mutex_t *restrict mutex);
1. pthread_cond_wait 这个函数的主要功能是等待条件变量cond,其实现逻辑是
先把调用线程加入到等待条件变量的列表,然后把解锁mutex,最后阻塞(suspending)调用线程;
2. 当有线程调用 pthread_cond_broadcast(signal) 发送信号后,pthread_cond_wait
解锁再次锁定 mutex,返回给调用线程;
为什么第一步过程中
Unlocking the mutex and suspending on the condition variable is done atomically.
解锁和阻塞要是原子操作???