[c++ concurrency]std::mutex

std::mutex

mutex代表的是MUTual EXclusive(禁止同时访问),通过memory barrier来实现。不同平台实现不一样,这里不深究了。

线程安全队列

#include <mutex>
#include <queue>

class threadSafe_queue {

    std::queue<int> rawQueue; // shared structure between all threads
    std::mutex m; // rawQueue's red door

public:

    int& retrieve_and_delete() {
        int front_value = 0; // if empty return 0
        m.lock();
        // From now on, the current thread is the only one that can access rawQueue
        if( !rawQueue.empty() ) {
            front_value = rawQueue.front();
            rawQueue.pop();
        }
        m.unlock();
        // other threads can lock the mutex now
        return front_value;
    };

    void push(int val) {
        m.lock();
        rawQueue.push(val);
        m.unlock();
    };
};

使用std::lock_guard,不需要主动unlock了

#include <mutex>
#include <queue>

class threadSafe_queue {

    std::queue<int> rawQueue; // shared structure between all threads
    std::mutex m; // rawQueue's red door

public:

    int& retrieve_and_delete() {
        int front_value = 0; // if empty return 0
        std::lock_guard<std::mutex> lg(m);
        // From now on, the current thread is the only one that can access rawQueue
        if( !rawQueue.empty() ) {
            front_value = rawQueue.front();
            rawQueue.pop();
        }
        return front_value;
    };  // other threads can lock the mutex now

    void push(int val) {
        std::lock_guard<std::mutex> lg(m);
        // from now on, the current thread is the only one that can access rawQueue
        rawQueue.push(val);
    }; // other threads can lock the mutex now
};

std::unique_lock可以通过unlock来解锁,同时不是去raii的优势

std::vector< int > vector; // shared between threads
std::mutex mutex; 

...

int function(...)
{
    ...
    std::unique_lock guard(mutex);
    Getting int from the shared vector.
    ...

    ...
    guard.unlock();
    Long, complicated computation with int. 
    This part does not depend on the vector. 
    ... 
}

[c++17]std::shared_mutex,std::shared_lock,std::unique_lock

#include <shared_mutex>
#include <vector>
std::shared_mutex door; //mutex declaration
std::vector<int> v;
int readVectorSize() {
    /* multiple threads can call this function simultaneously
     * no writing access allowed when sl is acquired */    

    std::shared_lock<std::shared_mutex> sl(door);
    return v.size();
}
void pushElement(int new_element) {
    /* exclusive access to vector guaranteed */

    std::unique_lock<std::shared_mutex> ul(door);
    v.push_back(new_element);
}

std::scoped_lock支持同时锁住多个互斥量

c++11

void deadLock(CriticalData& a, CriticalData& b){
    std::unique_lock<mutex> guard1(a.mut, std::defer_lock);
    std::unique_lock<mutex> guard2(b.mut, std::defer_lock);
    std::lock(guard1, guard2);
    // do something with a and b (critical region)
}

c++17

void deadLock(CriticalData& a, CriticalData& b){
    std::scoped_lock(a.mut, b.mut);
    // do something with a and b (critical region
}

原文链接: https://www.cnblogs.com/ConfuciusPei/p/12357152.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    [c++ concurrency]std::mutex

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/331479

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年3月1日 下午6:03
下一篇 2023年3月1日 下午6:04

相关推荐