c++11 多線程編程——如何實現(xiàn)線程安全隊列
線程安全隊列的接口文件如下:
#include <memory>
template<typename T>
class threadsafe_queue {
public:
threadsafe_queue();
threadsafe_queue(const threadsafe_queue&);
threadsafe_queue& operator=(const threadsafe_queue&) = delete;
void push(T new_value);
bool try_pop(T& value);
std::shared_ptr<T> try_pop();
void wait_and_pop(T& value);
std::shared_ptr<T> wait_and_pop();
bool empty() const;
};
push函數(shù)
push()函數(shù)實現(xiàn)向隊列添加數(shù)據(jù)的功能。添加數(shù)據(jù)后,使用std::condition_variable的notify_one通知取數(shù)據(jù)時被阻塞的線程。
void push(T tValue) {
std::shared_ptr<T> data(std::make_shared<T>(std::move(tValue)));
std::lock_guard<std::mutex> lk(mut);
data_queue.push(data);
data_con.notify_one();
}
wait_and_pop函數(shù)
wait_and_pop()函數(shù)實現(xiàn)從隊列取數(shù)據(jù)的功能,當隊列為空時,線程被掛起,等待有數(shù)據(jù)時被喚醒。
注意,這兩個函數(shù)中沒有使用std::lock_guard,而是使用std::unique_lock,這是為什么呢?
這是因為std::condition_variable的wait函數(shù)會首先檢測條件data_queue.empty()是否滿足,如果隊列為空,wait函數(shù)會釋放mutex,并被掛起;當有新的數(shù)據(jù)進入隊列,std::condition_variable的wait函數(shù)會被喚醒,重新嘗試獲取mutex,然后檢測隊列是否為空,如果隊列非空,則繼續(xù)向下執(zhí)行。由于函數(shù)的執(zhí)行過程存在鎖的釋放和重新獲取,所以沒有使用std::lock_guard,而是選擇了std::unique_lock。
void wait_and_pop(T& value) {
std::unique_lock<std::mutex> lk(mut);
data_cond.wait(lk,[this]{return !data_queue.empty();});
value=data_queue.front();
data_queue.pop();
}
std::shared_ptr<T> wait_and_pop() {
std::unique_lock<std::mutex> lk(mut);
data_cond.wait(lk,[this]{return !data_queue.empty();});
std::shared_ptr<T> res(std::make_shared<T>(data_queue.front()));
data_queue.pop();
return res;
}
try_pop函數(shù)
try_pop函數(shù)提供非阻塞調(diào)用下的彈出隊列(queue)的功能。彈出成功返回true或者非空shared_ptr,失敗則返回false或者nullptr。
bool try_pop(T& value) {
std::lock_guard<std::mutex> lk(mut);
if(data_queue.empty()) {
return false;
}
value = data_queue.front();
data_queue.pop();
return true;
}
std::shared_ptr<T> try_pop() {
std::lock_guard<std::mutex> lk(mut);
if(data_queue.empty()) {
return std::shared_ptr<T>();
}
std::shared_ptr<T> res(std::make_shared<T>(data_queue.front()));
data_queue.pop();
return res;
}
empty函數(shù)
bool empty() const {
std::lock_guard<std::mutex> lk(mut);
return data_queue.empty();
}
這里注意,empty()是const類型的成員函數(shù),表明它聲明自己并不改變?nèi)魏纬蓡T變量,但是mutex lock是一個mutating opertation,所以必須要將mut聲明為mutable類型(mutable std::mutex mut)。
完整代碼如下:
#include <queue>
#include <memory>
#include <mutex>
#include <condition_variable>
template<typename T>
class threadsafe_queue {
private:
mutable std::mutex mut;
std::queue<T> data_queue;
std::condition_variable data_cond;
public:
threadsafe_queue(){}
threadsafe_queue(threadsafe_queue const& other) {
std::lock_guard<std::mutex> lk(other.mut);
data_queue=other.data_queue;
}
void push(T new_value) {
std::lock_guard<std::mutex> lk(mut);
data_queue.push(new_value);
data_cond.notify_one();
}
void wait_and_pop(T& value) {
std::unique_lock<std::mutex> lk(mut);
data_cond.wait(lk,[this]{return !data_queue.empty();});
value=data_queue.front();
data_queue.pop();
}
std::shared_ptr<T> wait_and_pop() {
std::unique_lock<std::mutex> lk(mut);
data_cond.wait(lk,[this]{return !data_queue.empty();});
std::shared_ptr<T> res(std::make_shared<T>(data_queue.front()));
data_queue.pop();
return res;
}
bool try_pop(T& value) {
std::lock_guard<std::mutex> lk(mut);
if(data_queue.empty())
return false;
value=data_queue.front();
data_queue.pop();
return true;
}
std::shared_ptr<T> try_pop() {
std::lock_guard<std::mutex> lk(mut);
if(data_queue.empty())
return std::shared_ptr<T>();
std::shared_ptr<T> res(std::make_shared<T>(data_queue.front()));
data_queue.pop();
return res;
}
bool empty() const {
std::lock_guard<std::mutex> lk(mut);
return data_queue.empty();
}
};
以上就是c++ 如何實現(xiàn)線程安全隊列的詳細內(nèi)容,更多關(guān)于c++ 線程安全隊列的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
詳解C語言整數(shù)和浮點數(shù)在內(nèi)存中的存儲
這篇文章主要介紹了C語言整數(shù)和浮點數(shù)在內(nèi)存中是如何存儲的,文中有詳細的代碼示例供大家參考,對大家了解學習C語言整數(shù)和浮點數(shù)在內(nèi)存中的存儲有一定的幫助,需要的朋友可以參考下2024-03-03
C++?OpenCV實現(xiàn)之實現(xiàn)紅綠燈識別
本文以實現(xiàn)行車過程當中的紅綠燈識別為目標,核心的內(nèi)容包括:OpenCV輪廓識別原理以及OpenCV紅綠燈識別的實現(xiàn)具體步驟,感興趣的可以了解一下2022-08-08
C語言數(shù)據(jù)結(jié)構(gòu)單鏈表接口函數(shù)全面講解教程
這篇文章主要為大家介紹了C語言數(shù)據(jù)結(jié)構(gòu)單鏈表所有接口函數(shù)的全面講解教程,有需要朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2021-10-10
C語言游戲必備:光標定位與顏色設(shè)置的實現(xiàn)方法
本篇文章是對c語言中光標定位與顏色設(shè)置的方法進行了詳細的分析介紹,需要的朋友參考下2013-05-05

