pthread_cond

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

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct ShareInt
{
int x;
pthread_mutex_t lock;
pthread_cond_t cond1;
pthread_cond_t cond2;
};
void *Thread1Entry(void *pv);
void *Thread2Entry(void *pv);
int main(int argc,char *argv[])
{
pthread_t tid1;
pthread_t tid2;
struct ShareInt st = {0};
int ret = 0;

pthread_mutex_init(&st.lock,NULL);
pthread_cond_init(&st.cond1,NULL);
pthread_cond_init(&st.cond2,NULL);

ret = pthread_create(&tid1,NULL,Thread1Entry,(void *)&st);
ret += pthread_create(&tid2,NULL,Thread2Entry,(void *)&st);
if(ret < 0)
{
	printf("pthread_create failed\n");
	return 1;
}

pthread_join(tid1,NULL); //用来等待一个线程的结束,线程间同步的操作
pthread_join(tid2,NULL);

printf("\n");	
pthread_mutex_destroy(&st.lock);
pthread_cond_destroy(&st.cond1);
pthread_cond_destroy(&st.cond2);
return 0;

}

void *Thread1Entry(void *pv) //线程调用函数
{
struct ShareInt *pst = (struct ShareInt *)pv;
int v = 0;

while(1)
{
	pthread_mutex_lock(&pst->lock);
	if(pst->x % 2 != 0)
	{
		pthread_cond_wait(&pst->cond1,&pst->lock);
		//pthread_cond_wait的工作流程可以总结为:unlock mutex,start waiting -> lock mutex。
	//unlock(mutex); condition_signal()顺序,condition_signal(); unlock(mutext)
	}
	pst->x++;
	printf("1");
	v++;
	pthread_mutex_unlock(&pst->lock);
	pthread_cond_signal(&pst->cond2);
	if(v > 9)
	{
		break;
	}
}
return (void *)0;

}

void *Thread2Entry(void *pv)
{
struct ShareInt *pst = (struct ShareInt *)pv;
int v = 0;

while(1)
{
	pthread_mutex_lock(&pst->lock);
	if(pst->x % 2 == 0)
	{
		pthread_cond_wait(&pst->cond2,&pst->lock);
	}
	pst->x++;
	printf("2");
	v++;
	pthread_mutex_unlock(&pst->lock);
	pthread_cond_signal(&pst->cond1);
	if(v > 9)
	{
		break;
	}
}
return (void *)0;

}

/*
何时要同步:
读取上一次可能由另一个线程写入的变量
写入下一次可能由另一线程读取的变
*/
//每个线程都是作为利用CPU的基本单位

//条件变量是利用线程间共享的全局变量进行同步的一种机制,主要包括两个动作:一个线程等待"条件变量的条件成立"而挂起;另一个线程使"条件成立"(给出条件成立信号)。为了防止竞争,条件变量的使用总是和一个互斥锁结合在一起。
//mutex互斥锁必须是普通锁或者适应锁,且在调用pthread_cond_wait()前必须由本线程加锁(pthread_mutex_lock()),而在更新条件等待队列以前,mutex保持锁定状态,并在线程挂起进入等待前解锁。在条件满足从而离开pthread_cond_wait()之前,mutex将被重新加锁,以与进入pthread_cond_wait()前的加锁动作对应。阻塞时处于解锁状态。
//激发条件有两种形式,pthread_cond_signal()激活一个等待该条件的线程,存在多个等待线程时按入队顺序激活其中一个;而pthread_cond_broadcast()则激活所有等待线程。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <error.h>
#include <fcntl.h>
#include <unistd.h>
#include <pthread.h>

char buffer[128];
int has_data = 0;
pthread_mutex_t mutex;
pthread_cond_t cond; //条件量
pthread_cond_t cond2;

void *read_buf(void *a) //线程调用函数
{

printf("the read id:%ld  the has_data%d",pthread_self(),has_data);  //pthread_selef 获得线程ID

do{

	pthread_mutex_lock(&mutex);
	if(has_data == 0)
	{
		pthread_cond_wait(&cond,&mutex);
	}
	else if(has_data == 1)
	{
		printf("the data: %s\n",buffer);
		
		has_data = 0;
		pthread_cond_signal(&cond2);
	}
	
	pthread_mutex_unlock(&mutex);
	printf("readout");

}while(strncmp(buffer,"#",1)!=0);
pthread_exit(NULL);

}
void write_buf(void)
{printf(“the write id:%ld the has_data%d”,pthread_self(),has_data);
char input[128];
do{
pthread_mutex_lock(&mutex);
if(has_data == 0)
{

		memset(input,0,128);
		
		printf("input data:\t");
		scanf("%s",input);
		sprintf(buffer,"%s",input);
		has_data = 1;
		pthread_cond_signal(&cond);
		
		
	}
	else if(has_data == 1)
	{
		pthread_cond_wait(&cond2,&mutex);
	}
	pthread_mutex_unlock(&mutex);
	
	
	printf("writeout:");

}while(strncmp(input,"#",1) !=0);
pthread_exit(NULL);

}

int main()
{
pthread_t id,id2;

pthread_cond_init(&cond, NULL);
pthread_cond_init(&cond2,NULL);
pthread_mutex_init(&mutex,NULL);

pthread_create(&id,NULL,(void*)read_buf,NULL);
pthread_create(&id2,NULL,(void*)write_buf,NULL);

pthread_join(id,NULL);
pthread_join(id2,NULL);

pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);
pthread_cond_destroy(&cond2);
return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值