-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.cpp
58 lines (54 loc) · 1.59 KB
/
example.cpp
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
56
57
58
#include <iostream>
#include <string>
#include <thread>
#include <sstream>
#include "messageQueue.h"
/////////////////////////////////////////////////////////////////////////////
// Name: messageQueue.h
// Purpose: messageQueue: Simple Message Queue for intra-thread communication
// Author: Mohd Radzi Ibrahim
// Modified by:
// Created: 13 July 2020
// Copyright: (c) MR Ibrahim
// Licence: GNU GENERAL PUBLIC LICENSE
/////////////////////////////////////////////////////////////////////////////
void TestQueue() {
MessageQueue<std::string> fromMain;
MessageQueue<std::string> fromThread;
std::thread t([&]() {
std::cout << "In thread...." << std::endl;
while (true) {
std::string msg;
if (!fromMain.Receive(msg)) {
std::cout << "Thread: nothing to receive" << std::endl;
break;
}
if (msg == "stop") {
break;
}
std::cout << ">>" << msg << std::endl;
fromThread.Send(msg + " processed.");
}
fromThread.Send("stop");
});
std::thread m([&]() {
for (int i = 0; i < 20; i++) {
std::stringstream ss;
ss << "Message No " << i;
fromMain.Send(ss.str());
}
fromMain.Send("stop");
std::string msg;
while (true) {
if (!fromThread.Receive(msg)) continue;
if (msg == "stop") break;
std::cout << "- " << msg << std::endl;
}
});
m.join();
t.join();
}
int main() {
TestQueue();
return 0;
}