博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Why does pthread_cond_signal not work?【转】
阅读量:6717 次
发布时间:2019-06-25

本文共 4153 字,大约阅读时间需要 13 分钟。

转自:

0

 

I am currently learing all around POSIX threads (pthread).

I now have created a simple program which increased a shared value by 7 until above 10000 then it should signal a condition to the next thread which decreases it by 3 until under 1000. At last it should divide the result through 2 and main should output the result.

my code

pthread_t threads[3];pthread_cond_t cond_a, cond_b;pthread_mutex_t mutex;int counter;void * worker_one();void * worker_two();void * worker_three();int main(int argv, const char ** argc) {    counter = 0;    pthread_cond_init(&cond_a, NULL);    pthread_cond_init(&cond_b, NULL);    pthread_mutex_init(&mutex, NULL);    pthread_create(&threads[0], NULL, worker_one, NULL);    pthread_create(&threads[1], NULL, worker_two, NULL);    pthread_create(&threads[2], NULL, worker_three, NULL);    pthread_join(threads[0], NULL);    pthread_join(threads[1], NULL);    pthread_join(threads[2], NULL);    printf("Value started at %d and ends with %d.\n", 0, counter);    return 0;}void * worker_one() {    printf("Worker one started.\n");    pthread_mutex_lock(&mutex);    printf("Worker one starting work.\n");    while (counter < 10000) {        counter += 7;    }    pthread_cond_signal(&cond_a);    printf("Worker one finished work with: %d.\n", counter);    pthread_mutex_unlock(&mutex);    pthread_exit(NULL);}void * worker_two() {    printf("Worker two started.\n");    pthread_mutex_lock(&mutex);    pthread_cond_wait(&cond_a, &mutex);    printf("Worker two starting work.\n");    while (counter > 1000)        counter -= 3;    printf("Worker two finished work with: %d.\n", counter);    pthread_cond_signal(&cond_b);    pthread_mutex_unlock(&mutex);    sleep(1);    pthread_exit(NULL);}void * worker_three() {    printf("Worker three started.\n");    pthread_mutex_lock(&mutex);    pthread_cond_wait(&cond_b, &mutex);    printf("Worker three starting work.\n");    counter /= 2;    printf("Worker three finished work with: %d.\n", counter);    pthread_mutex_unlock(&mutex);    pthread_exit(NULL);}

For some reason the whole execution hangs around the first thread. The signal is also fired but thread two does not react.

Can somebody tell me what I am doing wrong?

 
Could somebody tell me why down vote? –  May 29 '13 at 16:23
    
I've moved the code from gist to SO. Don't just link to code on an external site, please include relevant code in the question. –  May 29 '13 at 16:25
    
ok did not knew anything about this –  May 29 '13 at 16:25

6 accepted

 

I have answered a similar question here: .

The problem is that you wait before even testing the condition you want to wait for is true. What happens is that thread 1 signals before thread 2 is waiting, therefore the signal is lost and thread 2 will be waiting forever.

In order to avoid this, first test what you want to wait for, then wait only if it's not here.

EDIT: Ok, here is a possible solution with only one mutex and one condtion (untested)

Thread 1:

 

pthread_mutex_lock(&mutex); while(thread_1_should_work == false){ // wait until the condition is satisfied  pthread_cond_wait(&cond, &mutex); }//at this point, we owe the mutex and we know thread_1_should_work is true; // do work thread_1_shoudl_work = false; thread_2_should_work = true; pthread_cond_broadcast(&cond); //wake up any waiting thread (if it's not their turn, they'll call wait again)pthread_mutex_unlock(&mutex);
Is the testing case a simple external flag or something pthread internal? I use the example from but there are no flags actually –  May 29 '13 at 16:33
 
 
@bodokaiser: you'll have to make up a variable, but a simple boolean flag will do. Spurious wakeups are a reality unfortunately. –  May 29 '13 at 16:35
 
@sixlettervariables so the main idea of conditions are just some more specific mutex signaling (with out the actual locking). Is this correct? –  May 29 '13 at 16:36
 
Yes, in your case is thread_is_working flag would work (except that it has more than 6 letters :) I think the answer that the linked answer can help. –  May 29 '13 at 16:38
 
Actually, wait() releases the mutex and yield the thread atomically so that nothing happens while the thread is being yield. –  May 29 '13 at 16:41

 

转载地址:http://nzumo.baihongyu.com/

你可能感兴趣的文章
语言的歧义
查看>>
dede后台空白或者登录以后空白,点注销以后也是空白的解决方式
查看>>
微软虚拟化之一Hyper-V 2.0的安装及基本配置
查看>>
Silverlight实用窍门系列:52.Silverlight中的MVVM框架极速入门(以MVVM Light Toolkit为例)...
查看>>
DNS服务-详解
查看>>
mysqldump结合脚本的备份方案
查看>>
httpd-2.4 基础配置图解及实现
查看>>
深入浅出分布式文件系统MogileFS集群
查看>>
nagios被监控端nrpe添加流量监控
查看>>
如何在ROS中使用PCL—数据格式(1)
查看>>
[cocos2d-x]动作+场景切换
查看>>
从传统运维到云运维演进历程之软件定义存储(五)下
查看>>
解决Druid设置Oracle的Clob字段时的小坑
查看>>
简单安装openwebmail
查看>>
【我们都爱Paul Hegarty】斯坦福IOS8公开课个人笔记46 Persistence持久化
查看>>
java删除文件夹
查看>>
delphi 学生管理系统总结
查看>>
HTML5
查看>>
SQL Server事务日志分析
查看>>
redis演练(9) redis Cluster 集群快速部署&failover情况
查看>>