• Linux--线程条件控制实现线程的同步


    **与条件变量相关的api:

    创建,销毁,触发,广播,等待
    条件变量是线程另一可用的同步机制。条件变量给多个线程提供了一个汇合的场所。条件变量与互斥变量一起使用时,允许线程以无竞争的方式等待特定的条件发生。
    条件本身是由互斥量保护的。线程在改变条件状态前必须首先锁住互斥量,其他线程在获得互斥量之前不会察觉到这种改变,因为必须锁定互斥量以后才能计算条件。
    1、创建,销毁

    //条件变量使用之前必须初始化,pthread_cond_init。
    //条件变量的销毁:pthread_cond_destroy.
    #include 
     
    int pthread_cond_init(pthread_cond_t *restrict cond, const pthread_condattr_t *restrict attr);
    int pthread_cond_destroy(pthread_cond_t cond);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

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

    #include 
     
    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,否则返回错误编码
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

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

    #include 
     
    int pthread_cond_signal(pthread_cond_t cond);
    int pthread_cond_broadcast(pthread_cond_t cond);
    //返回:若成功返回0,否则返回错误编号
    
    • 1
    • 2
    • 3
    • 4
    • 5

    这两个函数可用用于通知线程条件已经满足。
    pthread_cond_signal函数将唤醒等待该条件的某个线程
    pthread_cond_broadcast函数将唤醒等待该条件的所有进程。

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

    4、例:
    线程 1 等待,线程 2 先执行,当 g_data == 3 时,唤醒线程 1 执行

    #include 
    #include 
    
    int g_data = 0;
    
    pthread_mutex_t mutex;
    pthread_cond_t cond;
    
    void *func1(void *arg)
    {
    	printf("t1: %ld thread is create!\n",(unsigned long)pthread_self());
    	printf("t1: param is %d \n",*((int *)arg));
    	
    	while(1){
    		pthread_cond_wait(&cond,&mutex);//等待条件变量
    		
    		if(g_data == 3){
    			printf("-----------t1 run !----------");
    		}
    		printf("t1: %d\n",g_data++);
    		sleep(1);
    		g_data = 0;
    		
    	}
    	
    }
    
    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);//触发条件变量
    		}
    		 
    		sleep(1);
    	}
    }
    
    int main()
    {
    	int ret;
    	int param = 100;
    	pthread_t t1;
    	pthread_t t2;
    	
    	pthread_mutex_t init(&mutex,NULL);
    	pthread_cond_init(&cond,NULL);//初始化条件变量
    	
    	
    	
    	int *pret = NULL;
    	
    	//创建线程
    	ret = pthread_create(&t1,NULL,func1,(void *)&param);//调用func1函数
    	if(ret == 0){
    //		printf("main: create t1 success! \n");
    	}
    	
    	ret = pthread_create(&t2,NULL,func1,(void *)&param);//调用func1函数
    	if(ret == 0){
    //		printf("main: create t2 success! \n");
    	}
    	
    //	printf("main: %ld\n",(unsigned long)pthread_self());
    	
    	
    	//等待
    	pthread_join(t1,(void **)&pret);
    	pthread_join(t2,(void **)&pret);
    	pthread_mutex_destroy (&mutex);
    	pthread_cond_destroy(&cond);//销毁条件变量
    	
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82

    运行结构:

    t2:0
    t2:1
    t2:2
    -----------t1 run !----------
    t1:3
    t2:0
    t2:1
    t2:2
    -----------t1 run !----------
    t1:3
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    5、测试
    在线程 1 添加测试代码:

    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;//测试
    	
    	while(1){
    		pthread_cond_wait(&cond,&mutex);//等待条件变量
    		
    		if(g_data == 3){
    			printf("-----------t1 run !----------");
    		}
    		printf("t1: %d\n",g_data++);
    		sleep(1);
    		g_data = 0;
    		if(cnt++ == 10){		//测试10次
    			exit(1);
    		}
    		
    	}
    	
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    把要测试的文件编译为可执行文件:
    gcc 文件 -lpthread -o 名文件:编译生成 名文件 可执行文件
    再编写测试文件

    int main(int argc,int **argv)
    {
    	int time=atoi(argv[1]);
    	int i=0;
     
    	for(i=0;i<time;i++){
    		system("./名文件");
    	}
    	return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    编译测试文件,生成a.out 可执行文件文件
    ./a.out 5 >> test.ret.txt:运行 5 次 名文件 输出结果追加入test.ret.txt文档中

    6、静态初始化:

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

    pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;
    pthread_cond_t cond=PTHREAD_COND_INITIALIZER;
    
    • 1
    • 2
  • 相关阅读:
    买阿里云服务器,实操搭建nginx+php+mysql+thinkphp5全过程(2)
    华为静态路由配置实验(超详细讲解+详细命令行)
    小程序源码:多功能喝酒神器-多玩法安装简单
    vue+three.js中使用Ammo.js
    【java核心技术】Java知识总结 -- 接口
    一文掌握 Go 文件的写入操作
    Java代码hello word
    【Java 基础语法】Java 的文件操作
    Qml使用cpp文件的信号槽
    ActiveReportsJS 3.2 中文版前端在线报表ActiveReportsJS
  • 原文地址:https://blog.csdn.net/weixin_48208102/article/details/133387821