pthread_cond_broadcast 不起作用?
我写了这个程序:
pthread_cond_t placeFootCondition;
pthread_mutex_t rf,lf;
void* rss(void* in){
while(1){
pthread_mutex_lock(&rf);
printf ( "rss: waiting for condition\n" );
pthread_cond_wait(&placeFootCondition,&rf);
printf ( " Right Single Support \n" );
sleep(1);
pthread_mutex_unlock(&rf);
}
}
void* lss(void* in){
while(1){
pthread_mutex_lock(&lf);
printf ( "lss: waiting for condition\n" );
pthread_cond_wait(&placeFootCondition,&lf);
printf ( "Left Single Support \n" );
sleep(1);
pthread_mutex_unlock(&lf);
}
}
int main(){
int rc;
pthread_mutex_init(&rf,NULL);
pthread_mutex_init(&lf,NULL);
pthread_cond_init(&placeFootCondition,NULL);
pthread_create(&t1,NULL,rss,NULL);
pthread_create(&t2,NULL,lss,NULL);
sleep(1);
rc=pthread_cond_broadcast(&placeFootCondition);
if(rc!=0) printf ( "rc=%i\n",rc );
sleep(5);
}
但是程序的输出是
rss: waiting for condition
lss: waiting for condition
Right Single Support
rss: waiting for condition
pthread_cond_broadcast(&placeFootCondition) 不应该唤醒所有线程???
I have write this program:
pthread_cond_t placeFootCondition;
pthread_mutex_t rf,lf;
void* rss(void* in){
while(1){
pthread_mutex_lock(&rf);
printf ( "rss: waiting for condition\n" );
pthread_cond_wait(&placeFootCondition,&rf);
printf ( " Right Single Support \n" );
sleep(1);
pthread_mutex_unlock(&rf);
}
}
void* lss(void* in){
while(1){
pthread_mutex_lock(&lf);
printf ( "lss: waiting for condition\n" );
pthread_cond_wait(&placeFootCondition,&lf);
printf ( "Left Single Support \n" );
sleep(1);
pthread_mutex_unlock(&lf);
}
}
int main(){
int rc;
pthread_mutex_init(&rf,NULL);
pthread_mutex_init(&lf,NULL);
pthread_cond_init(&placeFootCondition,NULL);
pthread_create(&t1,NULL,rss,NULL);
pthread_create(&t2,NULL,lss,NULL);
sleep(1);
rc=pthread_cond_broadcast(&placeFootCondition);
if(rc!=0) printf ( "rc=%i\n",rc );
sleep(5);
}
But the output of the program is
rss: waiting for condition
lss: waiting for condition
Right Single Support
rss: waiting for condition
pthread_cond_broadcast(&placeFootCondition) should not wake up all threads???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这就是你正在做的事情:
这是标准关于使用的内容不同的互斥锁:
最重要的是,在等待条件变量时,每个线程都应该使用相同的互斥锁。
Here is what you are doing:
Here is what the standard says about using different mutexes:
Bottom line, from every thread you should use the same mutex when waiting on a condition variable.
除了不正确地使用不同的互斥体之外,您的程序也不遵循任何逻辑。条件变量表示什么条件? (这称为“谓词”。)如果没有谓词,您将迷失唤醒。
pthread_cond_wait
函数尽管有其名称,但它并不是一个条件等待。这是无条件等待某个条件。仅当需要等待条件时才必须调用它。假设你和你姐姐共用一辆车,而你姐姐借了车,你就不能使用这辆车,所以你等着你姐姐回家。好吧,如果她已经回家了,你就要等很长时间了! (如果她回家了,但当你到达车旁时,她和车都不见了,你必须再次等待。)
模式是(对于你):
对于你的妹妹:
请注意,必须保护汽车的状况由单个共享互斥体。汽车的状态是谓词,它受互斥体保护并由线程共享。
另请注意,我们在且仅当等待条件时才调用 pthread_cond_wait。当汽车空闲时,我们不会等待。我们一直在等待,以防她在我们抢到车之前再次抢到车。
In addition to the incorrect use of different mutexes, your program follows no logic. What is the condition the condition variable indicates? (This is called the 'predicate'.) Without a predicate, you will get lost wakeups.
The
pthread_cond_wait
function, despite its name, is not a conditional wait. It is an unconditional wait for a condition. You must call it only when the condition is to be waited for.Suppose you and your sister share a car, and you can't use the car when your sister has borrowed it, so you wait for your sister to come home. Well, if she's already home, you're going to be waiting a long time! (And if she comes home but by the time you get to the car she and the car are gone, you have to wait again.)
The pattern is (for you):
and for your sister:
Notice that the car's condition must be protected by a single, shared mutex. The state of the car is the predicate, and it is protected by the mutex and shared by the threads.
Notice also that we call
pthread_cond_wait
while, and only when, the condition is to be waited for. We do not wait when the car is free. And we keep waiting in case she grabbed the car again before we could grab it.