c++11 多線程編程——如何實(shí)現(xiàn)線程安全隊(duì)列
線程安全隊(duì)列的接口文件如下:
#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ù)實(shí)現(xiàn)向隊(duì)列添加數(shù)據(jù)的功能。添加數(shù)據(jù)后,使用std::condition_variable的notify_one通知取數(shù)據(jù)時(shí)被阻塞的線程。
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ù)實(shí)現(xiàn)從隊(duì)列取數(shù)據(jù)的功能,當(dāng)隊(duì)列為空時(shí),線程被掛起,等待有數(shù)據(jù)時(shí)被喚醒。
注意,這兩個(gè)函數(shù)中沒有使用std::lock_guard,而是使用std::unique_lock,這是為什么呢?
這是因?yàn)閟td::condition_variable的wait函數(shù)會(huì)首先檢測條件data_queue.empty()是否滿足,如果隊(duì)列為空,wait函數(shù)會(huì)釋放mutex,并被掛起;當(dāng)有新的數(shù)據(jù)進(jìn)入隊(duì)列,std::condition_variable的wait函數(shù)會(huì)被喚醒,重新嘗試獲取mutex,然后檢測隊(duì)列是否為空,如果隊(duì)列非空,則繼續(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)用下的彈出隊(duì)列(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是一個(gè)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++ 如何實(shí)現(xiàn)線程安全隊(duì)列的詳細(xì)內(nèi)容,更多關(guān)于c++ 線程安全隊(duì)列的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
詳解C語言整數(shù)和浮點(diǎn)數(shù)在內(nèi)存中的存儲(chǔ)
這篇文章主要介紹了C語言整數(shù)和浮點(diǎn)數(shù)在內(nèi)存中是如何存儲(chǔ)的,文中有詳細(xì)的代碼示例供大家參考,對大家了解學(xué)習(xí)C語言整數(shù)和浮點(diǎn)數(shù)在內(nèi)存中的存儲(chǔ)有一定的幫助,需要的朋友可以參考下2024-03-03
用C語言實(shí)現(xiàn)圣誕樹(簡易版+進(jìn)階版)
大家好,本篇文章主要講的是用C語言實(shí)現(xiàn)圣誕樹(簡易版+進(jìn)階版),感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽2021-12-12
C++?OpenCV實(shí)現(xiàn)之實(shí)現(xiàn)紅綠燈識(shí)別
本文以實(shí)現(xiàn)行車過程當(dāng)中的紅綠燈識(shí)別為目標(biāo),核心的內(nèi)容包括:OpenCV輪廓識(shí)別原理以及OpenCV紅綠燈識(shí)別的實(shí)現(xiàn)具體步驟,感興趣的可以了解一下2022-08-08
C語言數(shù)據(jù)結(jié)構(gòu)單鏈表接口函數(shù)全面講解教程
這篇文章主要為大家介紹了C語言數(shù)據(jù)結(jié)構(gòu)單鏈表所有接口函數(shù)的全面講解教程,有需要朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2021-10-10
C語言游戲必備:光標(biāo)定位與顏色設(shè)置的實(shí)現(xiàn)方法
本篇文章是對c語言中光標(biāo)定位與顏色設(shè)置的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05

