中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當(dāng)前位置: 首頁 > news >正文

動態(tài)網(wǎng)站完整版百度pc網(wǎng)頁版

動態(tài)網(wǎng)站完整版,百度pc網(wǎng)頁版,北京快三是官方的嗎,網(wǎng)站設(shè)計(jì)中的日歷怎么做1 背景 STL的容器不是線程安全的,我們經(jīng)常會有需求要求數(shù)據(jù)結(jié)構(gòu)線程安全,比如寫生產(chǎn)者消費(fèi)者模型的時候,就要求隊(duì)列線程安全。利用std::queue和C線程標(biāo)準(zhǔn)庫的一些組件(mutex,condition_variable)&#xff…

1 背景

  • STL的容器不是線程安全的,我們經(jīng)常會有需求要求數(shù)據(jù)結(jié)構(gòu)線程安全,比如寫生產(chǎn)者消費(fèi)者模型的時候,就要求隊(duì)列線程安全。
  • 利用std::queue和C++線程標(biāo)準(zhǔn)庫的一些組件(mutex,condition_variable),可以寫一個線程安全的隊(duì)列ConcurrenceQueue。

2 思路梳理

需要4個函數(shù)

  • push,入隊(duì);
  • pop,出隊(duì)并返回原來對頭的元素,如果為隊(duì)空則阻塞
  • tryPop,出隊(duì)并返回原來對頭的元素,如果隊(duì)空返回空(使用智能指針作返回類型),非阻塞
  • empty,返回是否為空,實(shí)則沒啥用,多線程條件下判空,下一瞬間另一線程就可能push進(jìn)去東西了。

3 實(shí)現(xiàn)代碼

#ifndef __CONCURRENCEQUEUE_H__
#define __CONCURRENCEQUEUE_H__
#include <mutex>
#include <condition_variable>
#include <deque>
#include <queue>
#include <memory>template<typename DATATYPE, typename SEQUENCE = std::deque<DATATYPE>>
class ConcurrenceQueue 
{
public:ConcurrenceQueue() = default;ConcurrenceQueue(const ConcurrenceQueue & other){std::lock_guard<std::mutex> lg(other.m_mutex);m_data = other.m_data;}ConcurrenceQueue(ConcurrenceQueue &&) = delete;ConcurrenceQueue & operator= (const ConcurrenceQueue &) = delete;~ConcurrenceQueue() = default;bool empty() const {std::lock_guard<std::mutex> lg(m_mutex);return m_data.empty();}void push(const DATATYPE & data) {std::lock_guard<std::mutex> lg(m_mutex);m_data.push(data);m_cond.notify_one();}void push(DATATYPE && data) {std::lock_guard<std::mutex> lg(m_mutex);m_data.push(std::move(data));m_cond.notify_one();}std::shared_ptr<DATATYPE> tryPop() {  // 非阻塞std::lock_guard<std::mutex> lg(m_mutex);if (m_data.empty()) return {};auto res = std::make_shared<DATATYPE>(m_data.front());m_data.pop();return res;}std::shared_ptr<DATATYPE> pop() {  // 非阻塞std::unique_lock<std::mutex> lg(m_mutex);m_cond.wait(lg, [this] { return !m_data.empty(); });auto res = std::make_shared<DATATYPE>(std::move(m_data.front()));m_data.pop();return res;}private:std::queue<DATATYPE, SEQUENCE> m_data;mutable std::mutex m_mutex;std::condition_variable m_cond;
};
#endif

?4 測試

全局的:

ConcurrenceQueue<int> g_queue;void producer() 
{    for (int i = 0; i < 100; ++i) {g_queue.push(i);std::this_thread::sleep_for(std::chrono::seconds(3));}
}void consumer1() 
{while (1) {std::printf("[1]  -------   %d\n", *g_queue.pop());}
}void consumer2() 
{while (1) {auto front = g_queue.tryPop();std::printf("[2]  -------   %d\n", front ? *front : -1);std::this_thread::sleep_for(std::chrono::seconds(1));}
}

測試 1:(消費(fèi)者阻塞式消費(fèi))

int main () 
{std::thread t1(producer);std::thread t2(consumer1);t1.join();t2.join();return 0;
}

測試 2:(消費(fèi)者非阻塞式消費(fèi),但要sleep輪詢)

int main () 
{std::thread t1(producer);std::thread t2(consumer2);t1.join();t2.join();return 0;
}

http://www.risenshineclean.com/news/43139.html

相關(guān)文章:

  • ??诜慨a(chǎn)網(wǎng)站建設(shè)最近發(fā)生的熱點(diǎn)事件
  • 廣州市公司網(wǎng)站建設(shè)公司在線培訓(xùn)app
  • 網(wǎng)站 建設(shè)需求上海aso蘋果關(guān)鍵詞優(yōu)化
  • 網(wǎng)站域名 設(shè)置快速網(wǎng)站seo效果
  • 怎樣做網(wǎng)站首頁圖片變換seo研究中心培訓(xùn)機(jī)構(gòu)
  • 做俄羅斯外貿(mào)的網(wǎng)站設(shè)計(jì)網(wǎng)址域名ip查詢
  • 潮汕網(wǎng)站建設(shè)antnw網(wǎng)頁設(shè)計(jì)需要學(xué)什么
  • 網(wǎng)站加速服務(wù)武漢seo網(wǎng)絡(luò)優(yōu)化公司
  • 做網(wǎng)站的學(xué)什么代碼海外推廣服務(wù)
  • 麗水微信網(wǎng)站建設(shè)哪家好滄州網(wǎng)絡(luò)推廣公司
  • 建立網(wǎng)站的詳細(xì)步驟營銷模式有哪些 新型
  • 香港空間建網(wǎng)站百度一下百度網(wǎng)頁版
  • html網(wǎng)站要怎么做簡單制作html靜態(tài)網(wǎng)頁
  • 眉山市住房和城鄉(xiāng)建設(shè)局網(wǎng)站西安推廣平臺排行榜
  • 還有哪些免費(fèi)的網(wǎng)站可以做H5優(yōu)化推廣什么意思
  • 南京響應(yīng)式網(wǎng)站制作搜索引擎營銷案例有哪些
  • 1高端網(wǎng)站建設(shè)百度sem競價托管公司
  • 做美食的網(wǎng)站有那一些2345中國最好的網(wǎng)址站
  • 云南網(wǎng)站做的好的公司自己怎么免費(fèi)做百度推廣
  • 做網(wǎng)站用哪些語言seo是什么縮寫
  • 網(wǎng)站推廣結(jié)束語長沙網(wǎng)站優(yōu)化公司
  • 網(wǎng)站后臺密碼忘了全國人大常委會
  • 在godaddy做網(wǎng)站貴嗎在線識別圖片
  • 做網(wǎng)站是怎么賺錢的違法百度關(guān)鍵詞搜索趨勢
  • 網(wǎng)站設(shè)計(jì)多少錢一個優(yōu)化設(shè)計(jì)一年級下冊數(shù)學(xué)答案
  • 夜間正能量不良網(wǎng)站入口不用下載什么是搜索關(guān)鍵詞
  • 網(wǎng)站建設(shè)案例簡介怎么寫西安百度關(guān)鍵詞推廣
  • 上海青浦房地產(chǎn)網(wǎng)站建設(shè)太原做網(wǎng)站的工作室
  • 營銷型企業(yè)網(wǎng)站項(xiàng)目策劃表武漢seo 網(wǎng)絡(luò)推廣
  • app下載官方網(wǎng)站seo軟件簡單易排名穩(wěn)定