C++11有关线程同步的使用

C++11有关线程同步的使用

 

本文链接:https://blog.csdn.net/fengxinlinux/article/details/76686829

互斥量和条件变量是控制线程同步的常用手段,用来保护多线程同时访问的共享数据。
c++11提供了这些操作,同时还提供了原子变量和一次调用的操作,用起来非常的方便。
我们在这里只介绍如何在C++中使用这些同步机制,有关概念的介绍我们就不在这里多说了。


互斥量

C++11中提供了如下4种语义的互斥量(mutex):

  • std::mutex:独占的互斥量,不能递归使用。
  • std::timed_mutex:带超时的独占互斥量,不能递归使用。
  • std::recursive_mutex:递归互斥量,不带超时功能。
  • std::recursive_timed_mutex:带超时的递归互斥量。

独占互斥量std::mutex

这些互斥量的基本接口很相似,一般用法是通过lock()方法来阻塞线程,直到获得互斥量的所有权为止。在线程获得互斥量并完成任务之后,就必须使用unlock()来解除对互斥量的占用,lock()和unlock()必须成对出现。try_lock()尝试锁定互斥量,如果成功则返回true,如果失败则返回false,它是非阻塞的。
std::mutex的基本用法代码如下:

#include<iostream>
#include<thread>
#include<mutex>
#include<chrono>
using namespace std;


std::mutex g_lock;

void func()
{
    g_lock.lock();

    std::cout<<"entered thread "<<std::this_thread::get_id()<<std::endl;
    std::this_thread::sleep_for(std::chrono::seconds(1));
    std::cout<<"leaving thread "<<std::this_thread::get_id()<<std::endl;

    g_lock.unlock();
}  

int main()
{
    std::thread t1(func);
    std::thread t2(func);
    std::thread t3(func);

    t1.join();
    t2.join();
    t3.join();

    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

使用lock_guard可以简化lock/unlock的写法,同时也更安全,因为lock_guard在构造时会自动锁定互斥量,而在退出作用域后进行析构时就会自动解锁,从而保证了互斥量的正确操作,避免忘记unlock操作,因此,应尽量用lokc_guard。lock_guard用到了RAII技术,这种技术在类的构造函数中分配资源,在析构函数中释放资源,保证资源在出了作用域之后就释放。上面的例子使用lock_guard后会更简洁,代码如下:

void func()
{
    lock_guard<std::mutex> locker(g_lock);   //出作用域之后自动解锁
    cout<<"entered thread "<<this_thread::get_id()<<endl;
    this_thread::sleep_for(std::chrono::seconds(1));
    cout<<"leaving thread "<<std::this_thread::get_id()<<endl;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

递归的独占互斥量std::recursive_mutex

递归锁允许同一线程多次获得该互斥锁,可以用来解决同一线程需要多次获取互斥量时死锁的问题。一个线程多次获取同一个互斥量时会发生死锁。要解决这个死锁的问题,一个简单的办法就是用递归锁:std::recursive_mutex,它允许同一线程多次获得互斥量。
代码示例:

#include<iostream>
#include<thread>
#include<mutex>
using namespace std;


struct Complex
{
    std::recursive_mutex mutex;
    int i;

    Complex():i(0){}

    void mul(int x)
    {
        std::lock_guard<std::recursive_mutex> lock(mutex);
        i*=x;
    }
    void div(int x)
    {
        std::lock_guard<std::recursive_mutex> lock(mutex);
        i/=x;

    }
    void both(int x,int y)
    {
        std::lock_guard<std::recursive_mutex> lock(mutex);
        mul(x);
        div(y);
    }
};

int main()
{
    Complex complex;
    complex.both(32,23);  //因为同一线程可以多次获取同一互斥量,不会发生死锁

    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

需要注意的是尽量不要使用递归锁好,主要原因如下:

  • 需要用到递归锁定的多线程互斥处理往往本身就是可以简化的,允许递归互斥很容易放纵复杂逻辑产生,从而导致一些多线程同步引起的晦涩问题。
  • 递归锁比起非递归锁,效率会低一些。
  • 递归锁虽然允许同一个线程多次获得同一互斥量,可重复获得的最大次数并未具体说明,一旦超过一定次数,再对lock进行调用就会抛出std::system错误。

带超时的互斥量

std::timed_mutex是超时的独占锁,std::recursive_timed_mutex是超时的递归锁,主要用在获取锁时超时等待功能,因为有时不知道获取锁需要多久,为了不至于一直在等待获取互斥量,就设置一个等待超时时间,在超时后还可以做其他事情。
std::timed_mutex比std::mutex多了两个超时获取锁的接口:try_lock_for和try_lock_until,这两个接口是用来设置获取互斥量的超时时间。
std::timed_mutex的基本用法如下代码:


#include<iostream>
#include<thread>
#include<mutex>
using namespace std;

std::timed_mutex mutex1;

void work()
{
    chrono::milliseconds timeout(100);

    while(true)
    {
        if(mutex1.try_lock_for(timeout))
        {
            cout<<this_thread::get_id()<<":do work with the mutex"<<endl;
            chrono::milliseconds sleepDuration(250);
            this_thread::sleep_for(sleepDuration);

            mutex1.unlock();
            this_thread::sleep_for(sleepDuration);

        }
        else
        {
            cout<<this_thread::get_id()<<": do work without mutex"<<endl;

            chrono::milliseconds sleepDuration(100);
            this_thread::sleep_for(sleepDuration);
        }
    }
}


int main()
{
    thread t1(work);
    thread t2(work);

    t1.join();
    t2.join();

    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

在上面的例子中,通过一个while循环不断地去获取超时锁,如果超时还没有获取到锁时就休眠100毫秒,再继续获取超时锁。
相比std::timed_mutex,std::recursive_timed_mutex多了递归锁的功能,允许同一线程多次获得互斥量。

 

条件变量

条件变量是C++11提供的另外一种用于等待的同步机制,它能阻塞一个或多个线程,直到收到另外一个线程发出的通知或者超时,才会唤醒当前阻塞的线程。条件变量需要和互斥量配合起来用。C++11提供了两种条件变量:

  • condition_variable,配合std::unique_lock<std::mutex>进行wait操作。
  • condition_variable_any,和任意带有lock、unlock语义的mutex搭配使用,比较灵活,但效率比condition_variable差一些。

可以看到condition_variable_any比condition_variable更灵活,因为它更通用,对所有的锁都适用,而condition_variable性能更好。

条件变量的使用过程如下:

  • 拥有条件变量的线程获取互斥量。
  • 循环检查某个条件,如果条件不满足,则阻塞直到条件满足;如果条件满足,则向下执行。
  • 某个线程满足条件执行完之后调用notify_one或notify_all唤醒一个或者所有的等待线程。

我们可以看一个经典的生产者-消费者的例子:

#include<iostream>
#include<thread>
#include<mutex>
#include<condition_variable>
#include<unistd.h>
using namespace std;


mutex m_mutex;   //互斥量
int s=0;         //共享资源
condition_variable m_notempty;   //条件变量

void producer()     //生产者
{
    while(1)
    {
        sleep(1);
        m_mutex.lock();  //上锁
        s++;
        cout<<"increase one ,s="<<s<<endl;
        m_mutex.unlock();
        m_notempty.notify_one();
    }
}


void consumer()     //消费者
{
    while(1)
    { 
        sleep(1);
        unique_lock<mutex> locker(m_mutex);  
        while(s==0)
        m_notempty.wait(locker);      //使用条件变量
        s--;
        cout<<"decrease one,s="<<s<<endl;


    }
}
int main()
{
    //创建线程
    thread thread1(producer);
    thread thread2(consumer);


    thread1.join();
    thread2.join();
}
  • 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

上面的例子中,while(s==0) m_notempty.wait(locker); ,这句代码的意思的是当s等于0的时候,阻塞直到条件满足时被唤醒。
我们也可以这么用, m_notempty.wait(locker,[]{return s>0;}); ,将判断条件放到函数里面,意思是wait将一直阻塞,知道判断条件满足时,被唤醒。

 

原子变量

C++11提供了一个原子类型std::atomic<T>,可以使用任意类型作为模板参数,C++11内置了整型的原子变量,可以更方便地使用原子变量,使用原子变量就不需要使用互斥量来保护该变量了,因为对该变量的操作保证其是原子的,是不可中断的。用起来更简洁。
要做一个计时器,使用mutex时,代码如下:


#include<iostream>
#include<mutex>
using namespace std;



struct Counter
{
    public:
    int value;
    std::mutex m_mutex;

    void increment()
    {
        std::lock_guard<std::mutex> lock(mutex);
        value++;
    }

    void decrement()
    {
        std::lock_guard<std::mutex> lock(mutex);
        value--;
    }

    int get()
    {
        return value;
    }
};

  • 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

如果使用原子变量,就不需要再定义互斥量了,使用更简便。

#include<iostream>
#include<mutex>
using namespace std;



struct Counter
{
    public:

    std::atomic<int> value;

    void increment()
    {
        value++;
    }

    void decrement()
    {
        value--;
    }

    int get()
    {
        return value;
    }
};

  • 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

 

call_one/once_flag的使用

为了保证在多线程环境中某个函数仅被调用一次,比如,需要初始化某个对象,而这个对象只能初始化一次时,就可以用std::call_once来保证函数在多线程环境中只被调用一次。使用std::call_once时,需要一个once_flag作为call_one的入参,它的用法比较简单。

#include<iostream>
#include<thread>
#include<mutex>
using namespace std;


std::once_flag flag;

void do_once()
{
    std::call_once(flag,[]{std::cout<<"Called once"<<endl;});
}


int main()
{
    std::thread t1(do_once);
    std::thread t2(do_once);
    std::thread t3(do_once);

    t1.join();
    t2.join();
    t3.join();
}
  • 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

运行结果:

Called once

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值