-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththread3.cpp
51 lines (39 loc) · 1.4 KB
/
thread3.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
/*
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>
#include <chrono>
using namespace std;
struct Sleeper {
// storing reference
int &i;
public:
Sleeper(int & i_): i(i_) {};
void operator() (int k) {
for (unsigned int j = 0; j <= 5; j++) {
this_thread::sleep_for(chrono::milliseconds(100));
i += k;
}
}
};
int main() {
int valSleeper = 1000;
cout << "valSleeper" << " " << valSleeper << endl;
thread t(Sleeper(valSleeper), 5);
// t.detach();
// cout << "valSleeper" << " " << valSleeper << endl; // will return 1000 since main doesn't wait
t.join();
cout << "valSleeper" << " " << valSleeper << endl; // will return 1030 since as main waits
// NOTE: detaching a thread and doing something by reference is a bad idea
return 0;
}