-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path12_managed_thread1.cpp
59 lines (46 loc) · 1.6 KB
/
12_managed_thread1.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
// Uses a wrapper class for std::thread
// Ensures safe destruction when an exception is throw
#include <thread>
#include <iostream>
// The object created with this class is move-only object
// So the constructor need to move its argument into this object
class thread_guard{
std::thread thr;
public:
// Constructor takes rvalue reference argument (std::thread is move-only)
explicit thread_guard(std::thread&& thr): thr(std::move(thr)){
}
// Destructor - join the thread if necessary
~thread_guard(){
if(thr.joinable())
thr.join();
}
// Delete copy operators prevent copying
thread_guard(const thread_guard&) = delete;
thread_guard& operator=(const thread_guard&) = delete;
// The move assignment operator is not synthesized
};
// Callable object - thread entry point
void hello()
{
std::cout<<"Hello Thread"<<std::endl;
}
int main(){
try{
// we have the task function
// We created the the thread in main
std::thread thr(hello);
// and then move it into a thread_guard object
// So this thread_guard is now responsible for making sure that this thread gets terminated properly
thread_guard tguard{std::move(thr)};
//thread_guard rguard{std::thread(hello)};
// Code which might throw an exception
throw std::exception();
// At some point the destructor will be called
// The thread_guard destructor will be called first and that will call join() if necessary
// Then the thread member will be destroyed, and that does not need to call join()
}// Calls ~thread_guard followed by ~thread
catch (std::exception& e){
std::cout<<"Exception caught: " << e.what()<<std::endl;
}
}