linux——条件控制实现线程的同步

与条件变量相关的api:

        条件变量是线程另一可用的同步机制。条件变量给多个线程提供了一个汇合的场所。条件变量与互斥变量一起使用时,允许线程以无竞争的方式等待特定的条件发生。

        条件本身是由互斥量保护的。线程在改变条件状态前必须首先锁住互斥量,其他线程在获得互斥量之前不会察觉到这种改变,因为必须锁定互斥量以后才能计算条件。

1、创建、销毁条件变量

#include <pthread.h>

int pthread_cond_init(pthread_cond_t *restrict cond, const pthread_condattr_t *restrict attr);
int pthread_cond_destroy(pthread_cond_t cond);

        条件变量使用之前必须初始化,pthread_cond_init。条件变量的销毁:pthread_cond_destroy.

        除非需要创建一个非默认属性的条件变量,否则pthread_cond_init函数attr参数可用设置为NULL。

2、等待

#include <pthread.h>

int pthread_cond_wait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex);
int pthread_cond_timedwait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex, cond struct timespec *restrict timeout);
//成功返回0,否则返回错误编码

        pthread_cond_wait等待条件变为真。如果在给定的时间内条件不能满足,那么会生成一个代表一个出错码的返回量。传递给pthread_cond_wait的互斥量对条件进行保护,调用者把锁住的互斥量传给函数。函数把调用线程放到等待条件的线程列表上,然后对互斥量解锁,这两个操作都是原子操作。这样就关闭了条件检查和线程进入休眠状态等待条件改变这两个操作之间的时间通道,这样线程就不会错过条件的任何变化。pthread_cond_wait返回时,互斥量再次被锁住。

        pthread_cond_timedwait,timeout指定了等待的时间,它是通过timespec结构指定。

3、触发

#include <pthread.h>

int pthread_cond_signal(pthread_cond_t cond);
int pthread_cond_broadcast(pthread_cond_t cond);
//返回:若成功返回0,否则返回错误编号

        这两个函数可用用于通知线程条件已经满足。

        pthread_cond_signal函数将唤醒等待该条件的某个线程

        pthread_cond_broadcast函数将唤醒等待该条件的所有进程。

   *注:要在改变条件状态之后再给线程发信号。

代码演示:线程1等待,线程2满足条件让线程1运行

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

//int pthread_create(pthread_t *restrict tidp, const pthread_attr_t *restrict attr, void *(*start_rtn)(void *), void *restrict arg);
pthread_mutex_t mutex;
pthread_cond_t cond;
int g_data=0;

void *func1(void *arg)
{
	printf("t1:%ld thread is create\n",(unsigned long)pthread_self());
	printf("t1:param is %d\n",*((int*)arg));

	pthread_mutex_lock(&mutex);
	while(1){
		pthread_cond_wait(&cond, &mutex);//等待
		printf("t1 quit=======================\n");

		printf("t1:%d\n",g_data);
		g_data=0;
		sleep(1);
	}
}
void *func2(void *arg)
{
        printf("t2:%ld thread is create\n",(unsigned long)pthread_self());
        printf("t2:param is %d\n",*((int*)arg));
	while(1){
		printf("t2:%d\n",g_data);
		pthread_mutex_lock(&mutex);
		g_data++;
		if(g_data==3){
			pthread_cond_signal(&cond);//触发
		}
		pthread_mutex_unlock(&mutex);
		sleep(1);
	}
}


int main()
{
	int ret;
	int param=100;
	pthread_t t1;
	pthread_t t2;
	
	pthread_mutex_init(&mutex,NULL);
	pthread_cond_init(&cond,NULL);//创建

	ret=pthread_create(&t1,NULL,func1,(void*)&param);	
	if(ret==0){
	//	printf("main:create t1 success\n");
	}
        ret=pthread_create(&t2,NULL,func2,(void*)&param);
        if(ret==0){
        //        printf("main:create t2 success\n");
        }

	
//	printf("main %ld \n",(unsigned long)pthread_self());

	pthread_join(t1,NULL);
	pthread_join(t2,NULL);

	pthread_mutex_destroy(&mutex);
	pthread_cond_destroy(&cond);//销毁

	return 0;
}

编译运行结果:

t1:139654858008320 thread is create
t1:param is 100
t2:139654849615616 thread is create
t2:param is 100
t2:0
t2:1
t2:2
t1 quit=======================
t1:3
t2:0
t2:1
t2:2
t1 quit=======================
t1:3
t2:0
t2:1
t2:2
t1 quit=======================
t1:3

一直往下循环

添加测试代码:

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

//int pthread_create(pthread_t *restrict tidp, const pthread_attr_t *restrict attr, void *(*start_rtn)(void *), void *restrict arg);
pthread_mutex_t mutex;
pthread_cond_t cond;
int g_data=0;

void *func1(void *arg)
{
	printf("t1:%ld thread is create\n",(unsigned long)pthread_self());
	printf("t1:param is %d\n",*((int*)arg));
	static int cnt=0;

	pthread_mutex_lock(&mutex);
	while(1){
		pthread_cond_wait(&cond, &mutex);
		printf("t1 quit=======================\n");

		printf("t1:%d\n",g_data);
		g_data=0;
		sleep(1);
		if(cnt++==10){
			exit(1);
		}
	}
}
void *func2(void *arg)
{
        printf("t2:%ld thread is create\n",(unsigned long)pthread_self());
        printf("t2:param is %d\n",*((int*)arg));
	while(1){
		printf("t2:%d\n",g_data);
		pthread_mutex_lock(&mutex);
		g_data++;
		if(g_data==3){
			pthread_cond_signal(&cond);
		}
		pthread_mutex_unlock(&mutex);
		sleep(1);
	}
}


int main()
{
	int ret;
	int param=100;
	pthread_t t1;
	pthread_t t2;
	
	pthread_mutex_init(&mutex,NULL);
	pthread_cond_init(&cond,NULL);

	ret=pthread_create(&t1,NULL,func1,(void*)&param);	
	if(ret==0){
	//	printf("main:create t1 success\n");
	}
        ret=pthread_create(&t2,NULL,func2,(void*)&param);
        if(ret==0){
        //        printf("main:create t2 success\n");
        }

	
//	printf("main %ld \n",(unsigned long)pthread_self());

	pthread_join(t1,NULL);
	pthread_join(t2,NULL);

	pthread_mutex_destroy(&mutex);
	pthread_cond_destroy(&cond);

	return 0;
}

gcc demo8.c -lpthread -o demo8:编译生成demo8可执行文件

vi test1.c:写测试程序: 

int main(int argc,int **argv)
{
	int time=atoi(argv[1]);
	int i=0;

	for(i=0;i<time;i++){
		system("./demo8");
	}
	return 0;
}

gcc test1.c生成a.out文件

./a.out 10 >> test.ret.txt:运行10次demo8输出结果导入test.ret.txt文档中

linux生产者&消费者问题

静态初始化:

以上互斥锁和条件都使用的动态初始化;静态初始化需要用到宏

代码演示:

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

//int pthread_create(pthread_t *restrict tidp, const pthread_attr_t *restrict attr, void *(*start_rtn)(void *), void *restrict arg);
pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond=PTHREAD_COND_INITIALIZER;
int g_data=0;

void *func1(void *arg)
{
	printf("t1:%ld thread is create\n",(unsigned long)pthread_self());
	printf("t1:param is %d\n",*((int*)arg));
	static int cnt=0;

	pthread_mutex_lock(&mutex);
	while(1){
		pthread_cond_wait(&cond, &mutex);
		printf("t1 quit=======================\n");

		printf("t1:%d\n",g_data);
		g_data=0;
		sleep(1);
		if(cnt++==10){
			exit(1);
		}
	}
}
void *func2(void *arg)
{
        printf("t2:%ld thread is create\n",(unsigned long)pthread_self());
        printf("t2:param is %d\n",*((int*)arg));
	while(1){
		printf("t2:%d\n",g_data);
		pthread_mutex_lock(&mutex);
		g_data++;
		if(g_data==3){
			pthread_cond_signal(&cond);
		}
		pthread_mutex_unlock(&mutex);
		sleep(1);
	}
}


int main()
{
	int ret;
	int param=100;
	pthread_t t1;
	pthread_t t2;
	
//	pthread_mutex_init(&mutex,NULL);
//	pthread_cond_init(&cond,NULL);

	ret=pthread_create(&t1,NULL,func1,(void*)&param);	
	if(ret==0){
	//	printf("main:create t1 success\n");
	}
        ret=pthread_create(&t2,NULL,func2,(void*)&param);
        if(ret==0){
        //        printf("main:create t2 success\n");
        }

	
//	printf("main %ld \n",(unsigned long)pthread_self());

	pthread_join(t1,NULL);
	pthread_join(t2,NULL);

	pthread_mutex_destroy(&mutex);
	pthread_cond_destroy(&cond);

	return 0;
}

静态初始化,一次初始化。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值