#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; /*初始化互斥锁*/
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;  //初始化条件变量
void *thread1(void *);
void *thread3(void *);
void *thread2(void *);
int i=1;
int main(void)
{
        pthread_t t_a;
        pthread_t t_b;
        pthread_t t_c;

pthread_create(&t_a,NULL,thread1,(void *)NULL);/*创建进程t_a*/
        pthread_create(&t_b,NULL,thread2,(void *)NULL); /*创建进程t_b*/
/**1: no sleep(2):
   2:have sleep(2)
*/
//        sleep(2);
        pthread_create(&t_c,NULL,thread3,(void *)NULL); /*创建进程t_b*/
        pthread_join(t_b, NULL);/*等待进程t_b结束*/
          pthread_join(t_c, NULL);/*等待进程t_c结束*/

pthread_mutex_destroy(&mutex);
        pthread_cond_destroy(&cond);
        exit(0);
}
void *thread1(void *junk)
{
    for(i=1;i<=9;i++)
    {
        printf("IN one\n");
        pthread_mutex_lock(&mutex);//
        if(i%3==0)
                pthread_cond_signal(&cond);/*条件改变,发送信号,通知t_b进程*/
        else  
                printf("thead1:%d\n",i);
        pthread_mutex_unlock(&mutex);//*解锁互斥量*/
printf("Up  Mutex\n");
        sleep(5);
   }
}
void *thread2(void *junk)
{
        while(i<9)
        {
                printf("IN two \n");
                    pthread_mutex_lock(&mutex);
                while(i%3!=0)
                {
                            printf("thread2...\n"); 
                        pthread_cond_wait(&cond,&mutex);/*等待*/
                        printf("thread2:%d ****\n",i);
                }
                printf("thread2:%d\n",i);
                pthread_mutex_unlock(&mutex);
                printf("thread2: Down  Mutex\n");
                sleep(5);
        }
}
void *thread3(void *junk)
{
        while(i<9)
{
                printf("IN three \n");
                pthread_mutex_lock(&mutex);
                while(i%6!=0)
                {
                        printf("thread3...\n");
                        pthread_cond_wait(&cond,&mutex);
                        printf("thread3:%d ****\n",i);
                }
                printf("thread3: %d\n",i);
                pthread_mutex_unlock(&mutex);
                printf("thread3:Down MUtex\n");
                sleep(5);
        }
}
/**  1: 当有sleep时,thread2先于thread3启动.thread2的wait先于thread3的wait.这时当thread1调用singal时,thread2的wait调用了。接着当i=6时,thread3调用了wait。在接着当i=9时,thread2调用了wait
    2: 当没有sleep时,thread3先于thread2启动: 这个时候可以看出while的效果(先thread3:3 ****再thread3...)。
    终结:在接受pthread_cond_singal时,线程有个优先排队的过程。
IN one
thead1:1
Up  Mutex
IN three
thread3...
IN two
thread2...
IN one
thead1:2
Up  Mutex
IN one
Up  Mutex
thread3:3 ****
thread3...
IN one
thead1:4
Up  Mutex
IN one
thead1:5
Up  Mutex
IN one
Up  Mutex
thread2:6 ****
thread2:6
thread2: Down  Mutex
IN one
thead1:7
Up  Mutex
IN two
thread2...
IN one
thead1:8
Up  Mutex
IN one
Up  Mutex
thread3:9 ****
thread3...

*/

pthread_cond_singal condition相关推荐

  1. 【java线程】锁机制:synchronized、Lock、Condition

    [Java线程]锁机制:synchronized.Lock.Condition 原创 2013年08月14日 17:15:55 标签:Java /多线程 74967 http://www.infoq. ...

  2. java race condition_java多线程(一)Race Condition现象及产生的原因

    什么是Race Condition 首先,什么是Race Condition呢,Race Condition中文翻译是竞争条件,是指多个进程或者线程并发访问和操作同一数据且执行结果与访问发生的特定顺序 ...

  3. Python标准库threading模块Condition原理浅析

    Python标准库threading模块Condition原理浅析 本文环境python3.5.2 threading模块Condition的实现思路 在Python的多线程实现过程中,在Linux平 ...

  4. icp mysql_MySQL · 特性分析 · Index Condition Pushdown (ICP)

    前言 上一篇文章 提过,我们在之后的文章中会从 optimizer 的选项出发,系统的介绍 optimizer 的各个变量,包括变量的原理.作用以及源码实现等,然后再进一步的介绍优化器的工作过程(SQ ...

  5. pandas使用apply函数基于条件(if condition)生成新的数据列

    pandas使用apply函数基于条件(if condition)生成新的数据列 目录 pandas使用apply函数基于条件(if condition)生成新的数据列

  6. ReentrantLock中的Condition(等待和唤醒)

    Condition 类的 awiat 方法和 Object 类的 wait 方法等效 Condition 类的 signal 方法和 Object 类的 notify 方法等效 Condition 类 ...

  7. bean ref的bean可以做判断吗_Spring Boot @Condition 注解,组合条件你知道吗

    写在前面 当我们构建一个 Spring 应用的时候,有时我们想在满足指定条件的时候才将某个 bean 加载到应用上下文中, 在Spring 4.0 时代,我们可以通过 @Conditional 注解来 ...

  8. SAP ME12 修改采购信息记录,系统提示:Condition type P000 does not allow supplementary conditions

    SAP ME12 修改采购信息记录,系统提示:Condition type P000 does not allow supplementary conditions 1,执行事务代码ME12,进入采购 ...

  9. SAP SD 基础知识之定价中的条件技术(Condition Technique in Pricing)

    SAP SD 基础知识之定价中的条件技术(Condition Technique in Pricing) 一,定价程序Pricing Procedure 所有定价中允许的条件类型都包含在定价程序中: ...

最新文章

  1. Idea groovy表生成实体类带注释
  2. python潜力开源项目_10大Python开源项目推荐(Github平均star2135)
  3. 谷歌迈出量子计算开源第一步,推出首个量子机器学习库TensorFlow Quantum
  4. vue学习笔记(三)
  5. Oracle中rownum用法警示
  6. winform 代码定义事件
  7. SAP Fiori Service Modeler
  8. C# WPF 表单更改提示
  9. 替换Quartus 自带编辑器 (转COM张)
  10. strcpy ,strncpy ,strlcpy地用法
  11. 值得关注的AI信息安全公司
  12. vuecli+axios的post请求传递参数异常
  13. 如何切换DNN编辑器
  14. ubuntu开机自启动脚本
  15. 001 - CMake 安装
  16. cmd文件闪退问题追踪办法
  17. 苹果又一次将国产手机遮羞布撕下了,证明了谁才是创新领导者
  18. 【Linux】排查进程、挖矿病毒查找
  19. LeetCode——110,判断平衡二叉树
  20. 【虹科】人工智能和工业相机助力瓶盖质量控制

热门文章

  1. linux脚本控制,linux控制脚本
  2. MFC两个复选框互斥设置
  3. python字典get计数_python字典中的get方法与setdefault方法
  4. 微软考虑将 Python 作为 Excel 官方脚本语言
  5. iOS自定义简易刷新视图(仿MJRefresh)
  6. PARSEC測试集的应用领域和working set的大小
  7. Codeforces Round #383 (Div. 1) C(二分图)
  8. Nginx 代理 WebSocket
  9. 杭电1754--I Hate It(线段树)
  10. 滑动换屏——Fragment