-
Notifications
You must be signed in to change notification settings - Fork 0
/
threads.cpp
32 lines (24 loc) · 986 Bytes
/
threads.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
/*
Observers
---------
(public member function) - joinable - checks whether the thread is joinable, i.e. potentially running in parallel context
(public member function) - get_id - returns the id of the thread
(public member function) - native_handle - returns the underlying implementation-defined thread handle
Operations
----------
(public member function) - join - waits for a thread to finish its execution
(public member function) - detach - permits the thread to execute independently from the thread handle
(public member function) - swap - swaps two thread objects
*/
#include <iostream>
#include <thread>
using namespace std;
int main() {
thread t([] {
cout << this_thread::get_id() << endl;
});
t.join();
// t.detach(); Potential risk : if the child outlive the main thread then std::cout will go away with the main thread so we'll undefined behaviour
// Do detach only when the thread has no references in the main thread
return 0;
}