tbb/concurrent_queue example

#include <iostream>
#include "tbb/concurrent_queue.h"
#include <thread>
using namespace std;
void worker1();
void worker2();
void worker3();

void consumer();

tbb::concurrent_queue<int> pq;
int main(){
    thread t1(worker1);
    t1.detach();
    thread t2(worker2);
    t2.detach();
    thread t3(worker3);
    t3.detach();
    thread t4(consumer);
    t4.detach();
    while(true);
}


void worker1(){
    while(true)
        pq.push(1);
}

void worker2(){
    while(true)
        pq.push(2);
}

void worker3(){
    while(true)
        pq.push(3);
}

void consumer(){
    int i;
    while(pq.try_pop(i))
        cout<<i<<endl;
}

result:結(jié)果比較均勻(截取的中間結(jié)果)

1
2
1
3
2
1
3
2
1
2
1
3
1
2
3
1
2
3
1
2
3
1
2
3
1
2
3
1
2
3
1
1
2
3
2
1
3
1
2
3
2
1
3
2
1
3
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1 CALayer IOS SDK詳解之CALayer(一) http://doc.okbase.net/Hell...
    Kevin_Junbaozi閱讀 5,336評(píng)論 3 23
  • Javascript有很多數(shù)組的方法,有的人有W3C的API,還可以去MDN上去找,但是我覺(jué)得API上說(shuō)的不全,M...
    頑皮的雪狐七七閱讀 4,493評(píng)論 0 6
  • 有人說(shuō)寂寞 會(huì)傳染 思念 有始有終 也有人說(shuō)寂寞 是一根金色弦 直達(dá)心靈深處 讓內(nèi)心的真實(shí),最接近人生的真諦 他們...
    江城妖怪閱讀 248評(píng)論 0 3
  • 沙巴回來(lái)已經(jīng)一個(gè)星期多了,無(wú)所事事的我天天在家睜眼閉眼都是王者榮耀,這幾天有很多人都來(lái)問(wèn)我沙巴旅行的一些事宜,我想...
    豬蹄兒Nowhere閱讀 1,946評(píng)論 4 2
  • “你在哪呢?” “還在老地方等你” 小西在車?yán)锍橹鵁煟戎”?,他們認(rèn)識(shí)差不多一年了,在一起斷斷續(xù)續(xù)也折騰了好幾個(gè)...
    cherishmoment閱讀 235評(píng)論 0 0

友情鏈接更多精彩內(nèi)容