-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadDetach.cpp
More file actions
34 lines (25 loc) · 832 Bytes
/
ThreadDetach.cpp
File metadata and controls
34 lines (25 loc) · 832 Bytes
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
/* DETACH NOTES
0. This is used to detach newly created thread from the parent thread.
1. Always check before detaching a thread that it is joinable otherwise we may end up double detaching and
double detach() will result into program termination.
2. If we have detached thread and main function is returning then the detached thread execution is suspended */
#include<iostream>
#include<thread>
#include<chrono>
using namespace std;
void PrintNumbers(int x)
{
while(x-->0)
cout<<" Swapnil "<<x<<endl;
//std::this_thread::sleep_for(chrono::seconds(5));
}
int main()
{
std::thread t1(PrintNumbers, 10);
cout<<"main thread"<<endl;
if(t1.joinable())
t1.detach();
cout<<"main after completion of thread"<<endl;
return 0;
}
// compile: g++ -std=c++11 ThreadDetach.cpp -pthread