当前位置: 技术问答>linux和unix
关于 互斥和条件变量 的疑问
来源: 互联网 发布时间:2016-07-16
本文导语: pthread_mutex_t count_lock; pthread_cond_t count_nonzero; unsigned count; decrement_count() { pthread_mutex_lock(&count_lock); while(count == 0) { pthread_cond_wait(&count_nonzero, &count_lock);//这里 ...
pthread_mutex_t count_lock;
pthread_cond_t count_nonzero;
unsigned count;
decrement_count() {
pthread_mutex_lock(&count_lock);
while(count == 0) {
pthread_cond_wait(&count_nonzero, &count_lock);//这里
count = count - 1;
}
pthread_mutex_unlock(&count_lock);
}
increment_count() {
pthread_mutex_lock(&count_lock);
if(count == 0) {
pthread_cond_signal(&count_nonzero);
count = count + 1;
}
pthread_mutex_unlock(&count_lock);
}
在
pthread_mutex_lock(&count_lock);锁住了count_lock之后
pthread_cond_wait(&count_nonzero, &count_lock);有把自己阻塞了
请问increment_count() 的pthread_mutex_lock(&count_lock);是怎么通过的
pthread_cond_t count_nonzero;
unsigned count;
decrement_count() {
pthread_mutex_lock(&count_lock);
while(count == 0) {
pthread_cond_wait(&count_nonzero, &count_lock);//这里
count = count - 1;
}
pthread_mutex_unlock(&count_lock);
}
increment_count() {
pthread_mutex_lock(&count_lock);
if(count == 0) {
pthread_cond_signal(&count_nonzero);
count = count + 1;
}
pthread_mutex_unlock(&count_lock);
}
在
pthread_mutex_lock(&count_lock);锁住了count_lock之后
pthread_cond_wait(&count_nonzero, &count_lock);有把自己阻塞了
请问increment_count() 的pthread_mutex_lock(&count_lock);是怎么通过的
|
|
DESCRIPTION
The pthread_cond_timedwait() and pthread_cond_wait() functions shall block on a condition variable. They
shall be called with mutex locked by the calling thread or undefined behavior results.
These functions atomically release mutex and cause the calling thread to block on the condition variable
cond; atomically here means "atomically with respect to access by another thread to the mutex and then the
condition variable". That is, if another thread is able to acquire the mutex after the about-to-block
thread has released it, then a subsequent call to pthread_cond_broadcast() or pthread_cond_signal() in
that thread shall behave as if it were issued after the about-to-block thread has blocked.
man 一下就知道了。
The pthread_cond_timedwait() and pthread_cond_wait() functions shall block on a condition variable. They
shall be called with mutex locked by the calling thread or undefined behavior results.
These functions atomically release mutex and cause the calling thread to block on the condition variable
cond; atomically here means "atomically with respect to access by another thread to the mutex and then the
condition variable". That is, if another thread is able to acquire the mutex after the about-to-block
thread has released it, then a subsequent call to pthread_cond_broadcast() or pthread_cond_signal() in
that thread shall behave as if it were issued after the about-to-block thread has blocked.
man 一下就知道了。
|
不对,pthread_cond_wait前要先锁互斥量,即调用pthread_mutex_lock(),pthread_cond_wait在把线程放进阻塞队列后,自动对mutex进行解锁,使得其它线程可以获得加锁的权利。这样其它线程才能对临界资源进行访问并在适当的时候唤醒这个阻塞的进程。当pthread_cond_wait返回的时候又自动给mutex加锁。
最后的
pthread_mutex_unlock(&count_lock);还是需要的!