线程中的 条件变量 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("\n1111111111     i is %d         111111111111\n",i);
        pthread_mutex_lock(&mutex);/*锁住互斥量*/
		printf("thread1: lock %d\n", __LINE__);
        if(i%3==0){
			printf("thread1:signal 1  line:%d   i is %d \n", __LINE__,i);
            pthread_cond_signal(&cond);/*条件改变,发送信号,通知t_b进程*/
			printf("thread1:signal 2  %d\n", __LINE__);
			sleep(1);
		}
        pthread_mutex_unlock(&mutex);/*解锁互斥量*/
		printf("thread1: unlock %d\n\n", __LINE__);
		sleep(1);
	}
}
void *thread2(void *junk)
{
    while(i<6)
    {
		
		printf("2222222222222 in pthread2 i is %d 222222222222222222 \n",i);
        pthread_mutex_lock(&mutex);
		printf("thread2: lock line:%d   i is %d\n", __LINE__,i);
		if(i%3!=0){
			printf("thread2: wait 1  %d\n", __LINE__);
            pthread_cond_wait(&cond,&mutex);/*解锁mutex,并等待cond改变*/
			
			
			
			printf("thread2: wait 2  %d\n", __LINE__);
			printf("thread2: i is  %d\n\n", i);
		}
        pthread_mutex_unlock(&mutex);
		printf("thread2: unlock %d\n\n", __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

 

posted @ 2016-10-25 17:33  慢伴拍的二叉树  阅读(1153)  评论(0编辑  收藏  举报