#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