-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcpp_thread_pthread.cc
More file actions
63 lines (53 loc) · 1.67 KB
/
cpp_thread_pthread.cc
File metadata and controls
63 lines (53 loc) · 1.67 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
58
59
60
61
62
63
#include <thread>
#include <mutex>
#include <iostream>
#include <chrono>
#include <cstring>
#include <pthread.h>
std::mutex iomutex;
void f(int num)
{
std::this_thread::sleep_for(std::chrono::seconds(1));
std::lock_guard<std::mutex> lk(iomutex);
std::cout << "Thread " << num << " pthread_t " << pthread_self() << std::endl;
}
int main()
{
std::thread t1(f, 1), t2(f, 2);
//t1.join(); t2.join();
//t1.detach(); t2.detach();
std::cout << "Thread 1 thread id " << t1.get_id() << std::endl;
std::cout << "Thread 2 thread id " << t2.get_id() << std::endl;
std::cout << "Thread 1 native handle " << t1.native_handle() << std::endl;
std::cout << "Thread 2 native handle " << t2.native_handle() << std::endl;
t1.join(); t2.join();
//t1.detach(); t2.detach();
}
/* Before thread join/detach:
Thread 1 thread id 140280111331072
Thread 2 thread id 140280102938368
Thread 1 native handle 140280111331072
Thread 2 native handle 140280102938368
Thread 1 pthread_t 140280111331072
Thread 2 pthread_t 140280102938368
*/
/* After thread join:
Thread 1 pthread_t 139811504355072
Thread 2 pthread_t 139811495962368
Thread 1 thread id thread::id of a non-executing thread
Thread 2 thread id thread::id of a non-executing thread
Thread 1 native handle 0
Thread 2 native handle 0
*/
/* Before thread detach:
Thread 1 thread id 140387472209664
Thread 2 thread id 140387463816960
Thread 1 native handle 140387472209664
Thread 2 native handle 140387463816960
*/
/* After thread detach:
Thread 1 thread id thread::id of a non-executing thread
Thread 2 thread id thread::id of a non-executing thread
Thread 1 native handle 0
Thread 2 native handle 0
*/