linux条件变量知识点总结

简介: linux条件变量知识点总结

与条件变量相关API

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


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


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


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

 除非需要创建一个非默认属性的条件变量,否则pthread_cont_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函数的工作方式与pthread_cond_wait函数类似,只是多了一个timeout。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函数将唤醒等待该条件的所有进程。


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


Linux线程开发中,通常对于互斥量、条件变量等都会提供两种初始化方式,分别是动态初始化和静态初始化。


一般来说变量的静态初始化是预编译时对变量进行初始化,而动态初始化,是在程序运行时,根据需要,动态分配空间并赋值


如:

pthread cond t cond;
//动态初始化: pthread cond init(&cond,NULL);
/静态初始化: pthread cond t = PTHREAD COND INITIALIZER;
pthread mutex_t;
//动态初始化: pthread mutex init(&mutex,NULL);
//静态初始化: pthread mutex tT= PTHREAD MUTEX INITIALIZER;
————————————————

                            版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
                        
原文链接:https://blog.csdn.net/m0_74712453/article/details/130902028


示例

(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);
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));
    static int cnt = 0;
    
    while(1){
        
        pthread_cond_wait(&cond,&mutex);
        printf("t1 run================================\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;
}

编译结果:

(2)线程条件控制实现线程同步(静态初始化)

#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);
int g_data = 0;
 
//静态初始化
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
 
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);
        printf("t1 run================================\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;
}

编译结果:与上面一样

相关文章
|
11天前
|
存储 网络协议 编译器
【干货总结】Linux C/C++面试知识点
Linux C/C++基础与进阶知识点,不仅用于面试,平时开发也用得上!
428 8
|
1月前
|
Shell Linux Perl
Linux|如何允许 awk 使用 Shell 变量
Linux|如何允许 awk 使用 Shell 变量
37 2
|
1月前
|
Linux Perl
Linux|了解如何使用 awk 内置变量
Linux|了解如何使用 awk 内置变量
17 1
|
1月前
|
开发框架 .NET Linux
2024年最全C# 图解教程 第5版 —— 第1章 C# 和 ,2024年最新终于有人把Linux运维程序员必学知识点全整理出来了
2024年最全C# 图解教程 第5版 —— 第1章 C# 和 ,2024年最新终于有人把Linux运维程序员必学知识点全整理出来了
2024年最全C# 图解教程 第5版 —— 第1章 C# 和 ,2024年最新终于有人把Linux运维程序员必学知识点全整理出来了
|
1月前
|
Unix Shell Linux
linux互斥锁(pthread_mutex)知识点总结
linux互斥锁(pthread_mutex)知识点总结
|
1月前
|
存储 Linux C++
linux信号量与PV操作知识点总结
linux信号量与PV操作知识点总结
|
1月前
|
Unix Linux C语言
|
23天前
|
Shell Linux
linux shell 脚本中变量截取
linux shell 脚本中变量截取
20 0
|
1月前
|
存储 Linux 程序员
【Linux-14】进程地址空间&虚拟空间&页表——原理&知识点详解
【Linux-14】进程地址空间&虚拟空间&页表——原理&知识点详解
|
1月前
|
安全 算法 Linux
【Linux 系统】多线程(线程控制、线程互斥与同步、互斥量与条件变量)-- 详解(下)
【Linux 系统】多线程(线程控制、线程互斥与同步、互斥量与条件变量)-- 详解(下)