-
Notifications
You must be signed in to change notification settings - Fork 1
/
spsc_main.cpp
62 lines (53 loc) · 2.31 KB
/
spsc_main.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
59
60
61
62
#include <iostream>
#include <sstream>
#include <string>
#include <thread>
#include <tuple>
#include "q/q_api.hpp"
#include "q/spsc.hpp"
// Alias for easier readability
using spsc_queue_type = spsc::circular_fifo<std::string>;
using sender_type = std::shared_ptr<queue_api::Sender<spsc_queue_type>>;
using receiver_type = std::shared_ptr<queue_api::Receiver<spsc_queue_type>>;
void produceMessages(queue_api::Sender<spsc::circular_fifo<std::string>>& sender, std::atomic<bool>& should_continue_working) {
std::vector<std::string> greetings = {"Hello", "Bonjour", "Tjena", "Ciao", "Hola", "Hallo", "Hei", "Aloha", "Shalom", "Namaste",
"Hello", "Bonjour", "Tjena", "Ciao", "Hola", "Hallo", "Hei", "Aloha", "Shalom", "Namaste"};
size_t counter = 0;
for (auto greet : greetings) {
++counter;
while (!sender.push(greet)) {
std::this_thread::yield(); // Simple strategy: yield and try again
}
}
should_continue_working.store(false);
std::ostringstream oss;
oss << __FUNCTION__ << " sent " << counter << " greetings\n";
std::cout << oss.str();
}
// we can use template signature to make it easier for the input arguments
template <typename ReceiverQ>
void consumeMessages(ReceiverQ& receiver, std::atomic<bool>& keep_working) {
std::string msg;
while (keep_working || !receiver.empty()) {
if (!receiver.pop(msg)) {
std::this_thread::yield();
} else {
std::cout << "Received: " << msg << "\n";
}
}
}
int main() {
// Create a circular_fifo SPSC queue with a dynamic size, determined at runtime initialization.
auto queue = queue_api::CreateQueue<spsc::circular_fifo<std::string>>(10);
// Get the sender and receiver endpoints of the queue
auto senderQ = std::get<queue_api::index::sender>(queue);
std::atomic<bool> keep_working{true};
std::thread producer_thread(produceMessages, std::ref(senderQ), std::ref(keep_working));
auto receiverQ = std::get<queue_api::index::receiver>(queue);
// similar as above, but showing how template calling can make the thread-function API easier.
std::thread consumer_thread(consumeMessages<decltype(receiverQ)>, std::ref(receiverQ), std::ref(keep_working));
// Wait for both threads to complete
producer_thread.join();
consumer_thread.join();
return 0;
}