• 线程中的 条件变量 pthread_cond_t


    #include <pthread.h>
    #include <stdio.h>
    #include <stdlib.h>
    pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;/*初始化互斥锁*/
    pthread_cond_t cond = PTHREAD_COND_INITIALIZER;/*初始化条件变量*/
    void *thread1(void *);
    void *thread2(void *);
    int i=1;
    int main(void)
    {
        pthread_t t_a;
        pthread_t t_b;
        pthread_create(&t_a,NULL,thread1,(void *)NULL);/*创建进程t_a*/
        pthread_create(&t_b,NULL,thread2,(void *)NULL); /*创建进程t_b*/
        pthread_join(t_a, NULL);/*等待进程t_a结束*/
        pthread_join(t_b, NULL);/*等待进程t_b结束*/
        pthread_mutex_destroy(&mutex);
        pthread_cond_destroy(&cond);
        exit(0);
    }
    void *thread1(void *junk)
    {
        for(i=1;i<=6;i++)
        {
    		printf("
    1111111111     i is %d         111111111111
    ",i);
            pthread_mutex_lock(&mutex);/*锁住互斥量*/
    		printf("thread1: lock %d
    ", __LINE__);
            if(i%3==0){
    			printf("thread1:signal 1  line:%d   i is %d 
    ", __LINE__,i);
                pthread_cond_signal(&cond);/*条件改变,发送信号,通知t_b进程*/
    			printf("thread1:signal 2  %d
    ", __LINE__);
    			sleep(1);
    		}
            pthread_mutex_unlock(&mutex);/*解锁互斥量*/
    		printf("thread1: unlock %d
    
    ", __LINE__);
    		sleep(1);
    	}
    }
    void *thread2(void *junk)
    {
        while(i<6)
        {
    		
    		printf("2222222222222 in pthread2 i is %d 222222222222222222 
    ",i);
            pthread_mutex_lock(&mutex);
    		printf("thread2: lock line:%d   i is %d
    ", __LINE__,i);
    		if(i%3!=0){
    			printf("thread2: wait 1  %d
    ", __LINE__);
                pthread_cond_wait(&cond,&mutex);/*解锁mutex,并等待cond改变*/
    			
    			
    			
    			printf("thread2: wait 2  %d
    ", __LINE__);
    			printf("thread2: i is  %d
    
    ", i);
    		}
            pthread_mutex_unlock(&mutex);
    		printf("thread2: unlock %d
    
    ", __LINE__);
    		sleep(1);
    	}
    }
    

      编译:

    gcc test.c -o test -lpthread

    逻辑:

    主函数 创建 两个线程 1,2


    线程1:累加 i ,每次都互斥锁 当i 是3的倍数的时候 发送 改变条件信号给线程2 ;


    线程2:当 i <6 时,不断互斥锁操作,当 不是3的倍数的时候等待 调节改变 信号,阻塞线程;


    1=1时 ;

    线程2:i 不 整除 3 阻塞等待 条件改变信号;
    线程1:正常累加 i=2


    i=2
    线程2:阻塞
    线程1:不整除 3 ,正常累加


    i=3
    线程2:阻塞
    线程1:整除 ,接受到发送 的条件改变信号;
    线程2 解锁,正常运行 睡一秒


    i=4
    线程2:i 等于四 阻塞
    线程1:不整除 3 ,正常累加

    i=5
    线程2:i 等于四 阻塞
    线程1:不整除 3 ,正常累加

    i=6
    线程2:接受到信号 解锁
    线程1:发送信号

    运行:


    2222222222222 in pthread2 i is 1 222222222222222222
    thread2: lock line:46 i is 1
    thread2: wait 1 48

    1111111111 i is 1 111111111111
    thread1: lock 27
    thread1: unlock 35


    1111111111 i is 2 111111111111
    thread1: lock 27
    thread1: unlock 35


    1111111111 i is 3 111111111111
    thread1: lock 27
    thread1:signal 1 line:29 i is 3
    thread1:signal 2 31
    thread1: unlock 35

    thread2: wait 2 53
    thread2: i is 3

    thread2: unlock 57

    2222222222222 in pthread2 i is 3 222222222222222222
    thread2: lock line:46 i is 3
    thread2: unlock 57


    1111111111 i is 4 111111111111
    thread1: lock 27
    thread1: unlock 35

    2222222222222 in pthread2 i is 4 222222222222222222
    thread2: lock line:46 i is 4
    thread2: wait 1 48

    1111111111 i is 5 111111111111
    thread1: lock 27
    thread1: unlock 35


    1111111111 i is 6 111111111111
    thread1: lock 27
    thread1:signal 1 line:29 i is 6
    thread1:signal 2 31
    thread1: unlock 35

    thread2: wait 2 53
    thread2: i is 6

    thread2: unlock 57

    参考链接:http://blog.csdn.net/zclongembedded/article/details/7337729

  • 相关阅读:
    YARN 多租户资源池配置
    HDFS NameNode HA 部署文档
    MySQL 触发器示例
    Apache ZooKeeper 单机、集群部署文档
    Apache Flume 安装文档、日志收集
    IBM X3650 M4 主板故障
    Apache Hive 安装文档
    Apache Hadoop 集群安装文档
    Cloudera Manger CDH 安装文档
    VMware 克隆网卡无法启动
  • 原文地址:https://www.cnblogs.com/hongzhunzhun/p/5997520.html
Copyright © 2020-2023  润新知