zoukankan      html  css  js  c++  java
  • 线程中的 条件变量 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

  • 相关阅读:
    技术晨读_2015_11_29
    mysql的timeout
    Gradle目录解析
    flexbox简介
    elasticsearch 查询(match和term)
    内存那些事
    elasticsearch 文档
    elasticsearch 集群
    elasticsearch中的API
    小菜的程序员道路(三)
  • 原文地址:https://www.cnblogs.com/hongzhunzhun/p/5997520.html
Copyright © 2011-2022 走看看