[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
}

猜你喜欢

转载自www.cnblogs.com/ConfuciusPei/p/12357152.html