-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathgraceful_shutdown.cpp
More file actions
60 lines (45 loc) · 1.5 KB
/
graceful_shutdown.cpp
File metadata and controls
60 lines (45 loc) · 1.5 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
56
57
58
59
60
#include <msd/channel.hpp>
#include <atomic>
#include <chrono>
#include <csignal>
#include <future>
#include <iostream>
#include <sstream>
#include <thread>
static std::atomic<bool> shutdown{false};
void handle_sigint(int)
{
std::cout << "Waiting for channel to drain...\n";
shutdown.store(true, std::memory_order_seq_cst);
}
// Graceful shutdown using a bounded thread-safe channel. It runs a producer that sends integers and a consumer that
// processes them. On Ctrl+C, it stops producing, closes the channel, and waits for the consumer to drain remaining
// messages before exiting.
int main()
{
std::signal(SIGINT, handle_sigint);
msd::channel<int> channel{10};
// Continuously read from channel until it's drained (closed and empty)
const auto consume = [&channel]() {
for (const int message : channel) {
std::stringstream stream;
stream << message << " (" << channel.size() << ")\n";
std::cout << stream.str();
std::this_thread::sleep_for(std::chrono::milliseconds{100});
}
};
const auto consumer = std::async(consume);
// Continuously write to channel until process shutdown is requested
const auto produce = [&channel]() {
static int inc = 0;
while (!shutdown.load(std::memory_order_seq_cst)) {
++inc;
channel << inc;
}
channel.close();
};
const auto producer = std::async(produce);
// Wait
consumer.wait();
producer.wait();
}