再谈条件变量—从入门到出家

C语言与CPP编程

共 2351字,需浏览 5分钟

 ·

2022-02-13 08:14

再谈条件变量—从入门到出家


C语言--条件变量

条件变量是在线程中以睡眠的方式等待某一条件的发生;

条件变量是利用线程间共享的全局变量进行同步的一种机制:

  • 一个线程等待"条件变量的条件成立"挂起
  • 另一个线程使"条件成立"

条件变量的使用总是和一个互斥锁结合在一起;

**作用:**使用条件变量可以以原子方式阻塞线程,直到某个特定条件为真为止


我们一般使用的函数是:

#include 
int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *cond_attr) ;
int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);
int pthread_cond_signal(pthread_cond_t *cond);
int pthread_cond_broadcast(pthread_cond_t *cond);
int pthread_cond_destroy(pthread_cond_t *cond);

案例一

代码:

#include 
#include 
#include 

pthread_t t1;
pthread_t t2;

pthread_mutex_t mutex;
pthread_cond_t cond;

int i=0;

voidProcess1(void* arg)
{
    while(1)
    {
        pthread_mutex_lock(&mutex);
        i++;
        if(i%5 == 0)
        {
            pthread_cond_signal(&cond);
        }
        else
        {
            printf("this is Process1\n");
        }
        pthread_mutex_unlock(&mutex);
        sleep(2);
    }
}


voidProcess2(void* arg)
{
    while(1)
    {
        pthread_mutex_lock(&mutex);
        pthread_cond_wait(&cond,&mutex);
        printf("this is Process2,i=%d\n",i);
        pthread_mutex_unlock(&mutex);
        sleep(2);
    }
}

int main()
{
    pthread_cond_init(&cond,NULL);
    pthread_mutex_init(&mutex,NULL);
    pthread_create(&t1,NULL,Process1,NULL);
    pthread_create(&t2,NULL,Process2,NULL);
    pthread_join(t1,NULL);
    pthread_join(t2,NULL);
    return 0;
}

结果:

root@iZuf67on1pthsuih96udyfZ:~/C++/C++_test/Progress/cond# ./test1 
this is Process1
this is Process1
this is Process1
this is Process1
this is Process2,i=5
this is Process1
this is Process1
this is Process1
this is Process1
this is Process2,i=10
this is Process1
this is Process1
^C

通过条件变量来控制线程的输出;


上述是在C语言中使用条件变量对线程进行一个控制,在C++中,在标准库中也提供了同样的机制,使用起来会比C语言的更加方便,但是原理还是一样的;

C++ 条件变量

C++标准库在< condition_variable >中

原则与C语言中类似

  • 包含< mutex >和< condition_variable >,声明一个mutex和一个condition_variable变量
  • 通知“条件已满足”的线程必须调用notify_one()或notify_all(),条件满足时唤醒处于等待中的一个条件变量;
  • 等待"条件被满足"的线程必须调用wait(),可以让线程在条件未被满足时陷入休眠状态,当接收到通知时被唤醒去处理相应的任务;

C++ 使用案例

#include 
#include 
#include 
#include 
#include 

using namespace std;

//全局条件变量
condition_variable cond;
mutex _mutex;
int count = 0;

void fun1()
{
    while(1)
    {
        count++;
        unique_locklock(_mutex);
        if(count%5 == 0)
        {
            cond.notify_one();
        }
        else
        {
            cout<<"this is fun1,count="<endl;
        }
        lock.unlock();
        sleep(1);
    }
}

void fun2()
{
    while(1)
    {
        unique_locklock(_mutex);
        cond.wait(lock);
        cout<<"this is fun2,count="<endl;
        lock.unlock();
        sleep(2);
    }
}

int main()
{
    thread t1(fun1);
    thread t2(fun2);
    t1.join();
    t2.join();
    return 0;
}

结果:

root@iZuf67on1pthsuih96udyfZ:~/C++/C++_test/Progress/cond# g++ -std=c++11 test2.cpp -o test2 -lpthread
root@iZuf67on1pthsuih96udyfZ:~/C++/C++_test/Progress/cond# ./test2
this is fun1,count=1
this is fun1,count=2
this is fun1,count=3
this is fun1,count=4
this is fun2,count=5
this is fun1,count=6
this is fun1,count=7
this is fun1,count=8
^C

到这里,基本回顾了C和C++中条件变量的用法,但是在实际应用中。又有设么用呢?

可能工作年限多的人,接触到的比较多,但是对于很多小白来说,或者刚毕业、刚参加工作的人来说,这点接触的就不是很多了,。这里来举一个例子说一下:

假如在某个项目中,需要用到的是多个线程,最简单的就是,用队列的时候,我们一边写一边读,总是要加锁的,但是,我们的线程就必须一直空转对队列进行检测是否有数据,但是这样往往会造成CPU使用率比较高,资源的浪费,这种情况下,我们就可以是使用条件变量,控制线程的循环,降低资源使用率;当然,也有很多场景值得去探索,也有很多技术可以解决这这个问题,希望大家一起探索前进;

问题思考

上面介绍了C和C++中的使用,但是其实还是有些疑问的

疑问:为什么pthread_cond_wait前加了锁,但是pthread_cond_signal还可以加锁?

首先看下我从网上找的一张图:

从这个图片中,我们发现了一个现象,pthread_cond_wait函数内存进行对锁的解锁,并且阻塞休眠操作,阻塞完成后,再次进行了加锁操作;也就是在pthread_cond_wait阻塞期间,pthread_cond_signal可以进行加锁和解锁操作,这里是不冲突的;

浏览 28
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报