C++11的互斥量

互斥量是一种同步原语,是一种线程同步的手段,用来保护多线程同时访问的共享数据。

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

1、std::mutex:独占的互斥量,不能递归使用。

2、std::mutex_mutex:带超时的独占互斥量,不能递归使用。

3、std::recursive_mutex:递归互斥量,不带超时功能。

4、std::recursive_timed_mutex:带超时的递归互斥量

独占互斥量std::mutex

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

cpp 复制代码
#include <iostream>
#include <thread>
#include <mutex>
#include <chrono>
using namespace std;

std::mutex g_lock;

void func()
{
    g_lock.lock();

    cout << "enter thread: " << std::this_thread::get_id() << endl;

    std::this_thread::sleep_for(std::chrono::seconds(1));

    cout << "leaving thread: " << std::this_thread::get_id() << endl;

    g_lock.unlock();
}

///g++ mutex.cpp -lpthread
int main()
{
    std::thread t1(func);
    std::thread t2(func);
    std::thread t3(func);

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

    return 0;
}

输出结果如下:

cpp 复制代码
enter thread: 140569127851776
leaving thread: 140569127851776
enter thread: 140568859412224
leaving thread: 140568859412224
enter thread: 140568590972672
leaving thread: 140568590972672

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

cpp 复制代码
#include <iostream>
#include <thread>
#include <mutex>
#include <chrono>
using namespace std;

std::mutex g_lock;

void func()
{
    std::lock_guard<std::mutex> locker(g_lock);///出了作用域之后自动解锁

    cout << "enter thread: " << std::this_thread::get_id() << endl;

    std::this_thread::sleep_for(std::chrono::seconds(1));

    cout << "leaving thread: " << std::this_thread::get_id() << endl;

}

///g++ mutex.cpp -lpthread
int main()
{
    std::thread t1(func);
    std::thread t2(func);
    std::thread t3(func);

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

    return 0;
}

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

递归锁允许同一个线程多次获得该互斥锁,可以用来解决同一个线程需要多次获取互斥量死锁的问题。在下面的代码中,一个线程多次获取同一个互斥量时会发生死锁。

cpp 复制代码
#include <iostream>
#include <thread>
#include <mutex>
#include <chrono>
using namespace std;

struct Complex
{
public:
    Complex(){i = 20;}

    void mul(int x)
    {
        printf("%s %s %d\n", __FILE__, __func__, __LINE__);
        g_mutex.lock();
        ///std::lock_guard<std::mutex> locker(g_mutex);
        i *= x;
        printf("%s %s %d\n", __FILE__, __func__, __LINE__);
        g_mutex.unlock();
    }

    void div(int x)
    {
        printf("%s %s %d\n", __FILE__, __func__, __LINE__);
        g_mutex.lock();
        ///std::lock_guard<std::mutex> locker(g_mutex);
        i /= x;
        printf("%s %s %d\n", __FILE__, __func__, __LINE__);
        g_mutex.unlock();
    }

    void both(int x, int y)
    {
        ///std::lock_guard<std::mutex> locker(g_mutex);
        g_mutex.lock();
        printf("%s %s %d\n", __FILE__, __func__, __LINE__);
        mul(x);
        div(y);
        g_mutex.unlock();
        printf("%s %s %d\n", __FILE__, __func__, __LINE__);
    }

private:
    int i;
    std::mutex g_mutex;
};


///g++ mutex.cpp -lpthread
int main()
{
    Complex complex;
    complex.both(2, 4);

    return 0;
}

这个例子运行起来就会发生死锁,因为在调用both时获取了互斥量,之后再调用mul又要获取相同的互斥量,但是这个互斥量已经被当前线程获取了,无法释放,这时就会发生死锁。要解决这个死锁的问题,一个简单的办法就是用递归锁:std::recursive_mutex,它允许同一个线程多次获得互斥量。

cpp 复制代码
#include <iostream>
#include <thread>
#include <mutex>
#include <chrono>
using namespace std;

struct Complex
{
public:
    Complex(){i = 20;}

    void mul(int x)
    {
        printf("%s %s %d\n", __FILE__, __func__, __LINE__);
        g_mutex.lock();
        ///std::lock_guard<std::recursive_mutex> locker(g_mutex);
        i *= x;
        printf("%s %s %d\n", __FILE__, __func__, __LINE__);
        g_mutex.unlock();
    }

    void div(int x)
    {
        printf("%s %s %d\n", __FILE__, __func__, __LINE__);
        g_mutex.lock();
        ///std::lock_guard<std::recursive_mutex> locker(g_mutex);
        i /= x;
        printf("%s %s %d\n", __FILE__, __func__, __LINE__);
        g_mutex.unlock();
    }

    void both(int x, int y)
    {
        ///std::lock_guard<std::recursive_mutex> locker(g_mutex);
        g_mutex.lock();
        printf("%s %s %d\n", __FILE__, __func__, __LINE__);
        mul(x);
        div(y);
        g_mutex.unlock();
        printf("%s %s %d\n", __FILE__, __func__, __LINE__);
    }

private:
    int i;
    std::recursive_mutex g_mutex;
};

void func()
{
    Complex complex;
    complex.both(2, 4);
}

///g++ mutex.cpp -lpthread
int main()
{
    thread t1(func);

    t1.join();

    return 0;
}

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

1、需要用到递归锁定的多线程互斥处理往往本身就是可以简化的,允许递归互斥很容易放纵复杂逻辑的产生,从而导致一些多线程同步引起的问题。

2、递归锁比起非递归锁,效率会低一些。

带超时的互斥量std::timed_mutex

std::timed_mutex是超时的独占锁,主要用在获取锁时增加超时等待功能,因为有时不知道获取锁需要多久,为了不至于一直在等待获取互斥量,就设置一个等待超时时间,在超时后还可以做其他事情。

std::timed_mutex比std::mutex多了两个超时获取锁的接口:try_lock_for和try_lock_until,这两个接口是用来设置获取互斥量的超时时间,使用时可以用while循环取不断地获取互斥量。std::timed_mutex的基本用法如下所示。

cpp 复制代码
#include <iostream>
#include <thread>
#include <mutex>
#include <chrono>
using namespace std;

std::timed_mutex g_mutex;

void work()
{
    std::chrono::milliseconds timeout(1000);

    while(true)
    {
        if (g_mutex.try_lock_for(timeout))
        {
            cout << std::this_thread::get_id() << ": do work with the mutex" << endl;

            std::chrono::milliseconds sleepDuration(5000);

            std::this_thread::sleep_for(sleepDuration);

            g_mutex.unlock();

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

            std::chrono::milliseconds sleepDuration(2000);

            std::this_thread::sleep_for(sleepDuration);
        }
    }
}


///g++ mutex.cpp -lpthread
int main()
{
    std::thread t1(work);

    std::thread t2(work);

    t1.join();

    t2.join();

    return 0;
}

在上面的例子中,通过一个while循环不断地去获取超时锁,如果超时还没有获取到锁就会休眠,再继续获取超时锁。

相关推荐
YY&DS5 分钟前
Qt Designer 自定义控件已提升后,如何修改提升类
开发语言·qt
Brilliantwxx7 分钟前
【C++】 深入理解红黑树:实现与原理全解
数据结构·c++·笔记·算法·青少年编程·红黑树
右耳朵猫AI15 分钟前
Rust技术周刊 2026年第19周
开发语言·后端·rust
Leweslyh25 分钟前
基于 Confucius 架构的无人集群网络控制原语解析
开发语言·网络·php
月落归舟37 分钟前
Java线程小记
java·开发语言
摇滚侠1 小时前
01 基础语法 JavaScript 入门到精通全套教程
开发语言·javascript·ecmascript
sleven fung1 小时前
Milvus 向量数据库
开发语言·数据库·python·langchain·milvus
大大杰哥1 小时前
Java 日志框架详解:SLF4J + Logback 从入门到实战
java·开发语言·logback
ylscode1 小时前
黑客利用 GHOSTYNETWORKS 和 OMEGATECH 托管 JS 恶意软件基础设施
开发语言·安全·php·安全威胁分析
莫等闲-1 小时前
leetcode42. 接雨水 leetcode84.柱状图中最大的矩形
数据结构·c++·算法·leetcode