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

1.条件变量

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

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

条件变量使用之前必须首先初始化,pthread_cond_t数据类型代表的条件变量可以用两种方式进行初始化,可以把常量PTHREAD_COND_INITIALIZER赋给静态分配的条件变量,但是如果条件变量是动态分配的,可以使用pthread_cond_destroy函数对条件变量进行去除初始化(deinitialize)。

2.创建及销毁条件变量

函数原型:

cpp 复制代码
#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);
// 返回:若成功返回0,否则返回错误编号

参数说明:

pthread_cond_init

  • cond:指向要初始化的条件变量的指针。
  • attr:指向条件变量属性的指针,可以为NULL。如果为NULL,则使用默认属性。

返回值:

  • 如果成功,返回0;否则返回错误码。

3.等待

函数原型:

cpp 复制代码
#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

  • cond:指向要等待的条件变量的指针。
  • mutex:指向互斥锁的指针,用于保护条件变量。

返回值:

  • 如果成功,返回0;否则返回错误码。

pthread_cond_timedwait

复制代码

参数说明:

  • cond:指向要等待的条件变量的指针。
  • mutex:指向互斥锁的指针,用于保护条件变量。
  • abstime:指向一个timespec结构体的指针,表示等待的最长时间。如果设置为NULL,则表示无限等待。

返回值:

  • 如果成功,返回0;否则返回错误码。

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

pthread_cond_wait返回时,互斥量再次被锁住。

pthread_cond_timedwait函数的工作方式与pthread_cond_wait函数类似,只是多了一个timeout。timeout指定了等待的时间,它是通过timespec结构指定。

4.触发
函数原型:

cpp 复制代码
#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函数将唤醒等待该条件的所有进程。

注意一定要在改变条件状态以后再给线程发信号。

代码:

cpp 复制代码
#include<stdio.h>
#include<pthread.h>
#include <unistd.h>
#include<stdlib.h>
int data = 0;//定义一个全局变量data 
pthread_mutex_t mutex; //还可以直接定义和初始化好 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER
pthread_cond_t cond; //pthread_cond_t cond = PTHREAD_COND_INITIALIZER
void *func1(void *arg)
{
        static int cnt = 0;
        while(1){
                pthread_cond_wait(&cond,&mutex);//收到信号
                printf("===========t1 tun============\n");

                printf("t1线程拿到data:%d\n",data);
                data = 0;
                sleep(1);

                if(cnt++ == 3){
                        printf("结束!\n");
                        exit(1);//达到条件,结束整个程序

                }
        }

}
void *func2(void *arg)
{

        while(1){
                printf("线程t2拿到 data = %d\n",data);
                pthread_mutex_lock(&mutex);//先拿到互斥锁,先运行
                data++;
                if(data == 3){
                   pthread_cond_signal(&cond);//达到条件,向线程t1发送信号
                }
                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);//创建线程t1
        if(ret == 0){
                //      printf("main:创建线程t1成功!\n");
        }

        ret = pthread_create(&t2,NULL,func2,(void *)&param);//创建线程t2
        if(ret == 0){
                //      printf("main:创建线程t2成功!\n");
        }

        pthread_join(t1,NULL);
        pthread_join(t2,NULL);
        pthread_mutex_destroy(&mutex);
        pthread_cond_destroy(&cond);
        return 0;
}

结果:

相关推荐
小O_好好学20 分钟前
CentOS 7文件系统
linux·运维·centos
x晕x1 小时前
Linux dlsym符号查找疑惑分析
linux·运维·服务器
活跃的煤矿打工人2 小时前
【星海saul随笔】Ubuntu基础知识
linux·运维·ubuntu
fasewer2 小时前
第五章 linux实战-挖矿 二
linux·运维·服务器
楚灵魈3 小时前
[Linux]从零开始的网站搭建教程
linux·运维·服务器
小小不董3 小时前
《Linux从小白到高手》理论篇:深入理解Linux的网络管理
linux·运维·服务器·数据库·php·dba
这可就有点麻烦了4 小时前
强化学习笔记之【TD3算法】
linux·笔记·算法·机器学习
DY009J4 小时前
深度探索Kali Linux的精髓与实践应用
linux·运维·服务器
程序员-珍4 小时前
虚拟机ip突然看不了了
linux·网络·网络协议·tcp/ip·centos
码农小白4 小时前
linux驱动:(22)中断节点和中断函数
linux·运维·服务器