Why does pthread_cond_signal not work?【转】

简介:

转自:http://stackoverflow.com/questions/16819169/why-does-pthread-cond-signal-not-work#

down vote favorite

 

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? – bodokaiser 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. – user7116 May 29 '13 at 16:25
    
ok did not knew anything about this – bodokaiser May 29 '13 at 16:25

down vote accepted

 

I have answered a similar question here: pthread condition variables on Linux, odd behaviour.

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 computing.llnl.gov/tutorials/pthreads but there are no flags actually – bodokaiser 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. – user7116 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? – bodokaiser 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. – Ben 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. – Ben May 29 '13 at 16:41

 












本文转自张昺华-sky博客园博客,原文链接:http://www.cnblogs.com/sky-heaven/p/6072858.html,如需转载请自行联系原作者

相关文章
|
存储 缓存 Java
在Spring Boot中使用缓存的技术解析
通过利用Spring Boot中的缓存支持,开发者可以轻松地实现高效和可扩展的缓存策略,进而提升应用的性能和用户体验。Spring Boot的声明式缓存抽象和对多种缓存技术的支持,使得集成和使用缓存变得前所未有的简单。无论是在开发新应用还是优化现有应用,合理地使用缓存都是提高性能的有效手段。
258 1
|
监控 Linux 测试技术
【 C/C++ 性能分析工具 CPU 采样分析器 perf 】掀开Linux perf性能分析的神秘面纱
【 C/C++ 性能分析工具 CPU 采样分析器 perf 】掀开Linux perf性能分析的神秘面纱
757 0
|
监控 安全 数据安全/隐私保护
1.2 PCI总线的信号定义
<div class="bct fc05 fc11 nbw-blog ztag"><div> <p style="TEXT-INDENT: 21pt;"><span lang="EN-US" xmllang="EN-US">PCI</span><span style="FONT-FAMILY: 宋体;">总线是一条共享总线,在一条</span><span lang="EN-US" xmlla
2108 0
|
6天前
|
存储 关系型数据库 分布式数据库
PostgreSQL 18 发布,快来 PolarDB 尝鲜!
PostgreSQL 18 发布,PolarDB for PostgreSQL 全面兼容。新版本支持异步I/O、UUIDv7、虚拟生成列、逻辑复制增强及OAuth认证,显著提升性能与安全。PolarDB-PG 18 支持存算分离架构,融合海量弹性存储与极致计算性能,搭配丰富插件生态,为企业提供高效、稳定、灵活的云数据库解决方案,助力企业数字化转型如虎添翼!
|
17天前
|
弹性计算 关系型数据库 微服务
基于 Docker 与 Kubernetes(K3s)的微服务:阿里云生产环境扩容实践
在微服务架构中,如何实现“稳定扩容”与“成本可控”是企业面临的核心挑战。本文结合 Python FastAPI 微服务实战,详解如何基于阿里云基础设施,利用 Docker 封装服务、K3s 实现容器编排,构建生产级微服务架构。内容涵盖容器构建、集群部署、自动扩缩容、可观测性等关键环节,适配阿里云资源特性与服务生态,助力企业打造低成本、高可靠、易扩展的微服务解决方案。
1326 7
|
5天前
|
存储 人工智能 Java
AI 超级智能体全栈项目阶段二:Prompt 优化技巧与学术分析 AI 应用开发实现上下文联系多轮对话
本文讲解 Prompt 基本概念与 10 个优化技巧,结合学术分析 AI 应用的需求分析、设计方案,介绍 Spring AI 中 ChatClient 及 Advisors 的使用。
298 129
AI 超级智能体全栈项目阶段二:Prompt 优化技巧与学术分析 AI 应用开发实现上下文联系多轮对话
|
4天前
|
监控 JavaScript Java
基于大模型技术的反欺诈知识问答系统
随着互联网与金融科技发展,网络欺诈频发,构建高效反欺诈平台成为迫切需求。本文基于Java、Vue.js、Spring Boot与MySQL技术,设计实现集欺诈识别、宣传教育、用户互动于一体的反欺诈系统,提升公众防范意识,助力企业合规与用户权益保护。