-
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.11 KB
/
Copy pathdemo22a-blocking-queue.cpp
File metadata and controls
55 lines (37 loc) · 1.11 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++ std threading are not supported by default.
So, I use mylib::BlockingQueue for this demonstration.
*/
#include <iostream>
#include <string>
#include <chrono>
#include <thread>
#include "mylib-blockingqueue.hpp"
using namespace std;
using namespace mylib;
void producer(BlockingQueue<string>* blkQueue) {
std::this_thread::sleep_for(std::chrono::seconds(2));
blkQueue->put("Alice");
std::this_thread::sleep_for(std::chrono::seconds(2));
blkQueue->put("likes");
std::this_thread::sleep_for(std::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() {
auto blkQueue = BlockingQueue<string>();
auto thProducer = std::thread(&producer, &blkQueue);
auto thConsumer = std::thread(&consumer, &blkQueue);
thProducer.join();
thConsumer.join();
return 0;
}