-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathjoin_detach_threads.cpp
More file actions
57 lines (49 loc) · 1.79 KB
/
join_detach_threads.cpp
File metadata and controls
57 lines (49 loc) · 1.79 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
// Demonstrates §2.7–2.8 of docs/multithreading.md: the older std::thread API
// where the caller is responsible for join() or detach(). New code should
// prefer std::jthread (see creating_and_terminating_threads.cpp).
#include <chrono>
#include <iostream>
#include <syncstream>
#include <thread>
#include <vector>
void process_frame(int id) {
std::this_thread::sleep_for(std::chrono::milliseconds(50));
std::osyncstream(std::cout) << " frame " << id << " done\n";
}
// Fleet pattern: spawn N workers, wait for all of them.
void joining_demo() {
std::cout << "joining_demo:\n";
std::vector<std::thread> workers;
for (int i = 0; i < 4; ++i)
workers.emplace_back(process_frame, i);
for (auto &t : workers)
t.join(); // forgetting either join() or detach() -> std::terminate()
}
// Detached threads run on their own. The runtime cleans them up when they
// finish — but if main exits first, they're killed mid-operation.
void detaching_demo() {
std::cout << "detaching_demo:\n";
std::thread t([] {
std::this_thread::sleep_for(std::chrono::milliseconds(20));
std::cout << " detached thread finished\n";
});
t.detach(); // gives up ownership; cannot join() afterwards.
// Give the detached thread a moment to finish so we actually see its output.
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
// joinable() guards against double-join / join-after-detach (which would
// both call std::terminate()).
void joinable_guard_demo() {
std::cout << "joinable_guard_demo:\n";
std::thread t(process_frame, 99);
if (t.joinable())
t.join();
if (t.joinable()) // false now — already joined.
t.join();
std::cout << " joinable() correctly returned false the second time\n";
}
int main() {
joining_demo();
detaching_demo();
joinable_guard_demo();
}