-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathdemo22a-blocking-queue.cpp
More file actions
55 lines (37 loc) · 1.13 KB
/
Copy pathdemo22a-blocking-queue.cpp
File metadata and controls
55 lines (37 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
BLOCKING QUEUES
Version A: A slow producer and a fast consumer
Blocking queues in C++ Boost threading are not supported by default.
So, I use mylib::BlockingQueue for this demonstration.
*/
#include <iostream>
#include <string>
#include <boost/chrono.hpp>
#include <boost/thread.hpp>
#include "mylib-blockingqueue.hpp"
using namespace std;
using namespace mylib;
void producer(BlockingQueue<string>* blkQueue) {
boost::this_thread::sleep_for(boost::chrono::seconds(2));
blkQueue->put("Alice");
boost::this_thread::sleep_for(boost::chrono::seconds(2));
blkQueue->put("likes");
boost::this_thread::sleep_for(boost::chrono::seconds(2));
blkQueue->put("singing");
}
void consumer(BlockingQueue<string>* blkQueue) {
string data;
for (int i = 0; i < 3; ++i) {
cout << "\nWaiting for data..." << endl;
data = blkQueue->take();
cout << " " << data << endl;
}
}
int main() {
BlockingQueue<string> blkQueue;
boost::thread thProducer(&producer, &blkQueue);
boost::thread thConsumer(&consumer, &blkQueue);
thProducer.join();
thConsumer.join();
return 0;
}