forked from mpavezb/cpp_concurrency
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path07_useful_api.cpp
More file actions
54 lines (46 loc) · 1.29 KB
/
07_useful_api.cpp
File metadata and controls
54 lines (46 loc) · 1.29 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
#include <chrono>
#include <cstdio>
#include <iostream>
#include <thread>
void foo() {
std::cout << "This thread id: " << std::this_thread::get_id() << std::endl;
}
void get_id_test() {
std::thread thread_1(foo);
std::thread thread_2(foo);
std::thread thread_3(foo);
std::cout << "thread_1 id: " << thread_1.get_id() << std::endl;
std::cout << "thread_2 id: " << thread_2.get_id() << std::endl;
std::cout << "thread_3 id: " << thread_3.get_id() << std::endl;
thread_1.join();
thread_2.join();
thread_3.join();
// ID of non-executing thread.
std::thread thread_4;
std::cout << "thread_4 id: " << thread_4.get_id() << std::endl;
}
void display_hardware_concurrency() {
std::cout << "Allowed max number of parallel threads : "
<< std::thread::hardware_concurrency() << std::endl;
}
thread_local int sample_local_integer = 0;
int sample_normal_integer = 0;
void thread_local_sample() {
auto fn = []() {
++sample_local_integer;
++sample_normal_integer;
printf("- local integer: %d, normal integer: %d\n", sample_local_integer,
sample_normal_integer);
};
std::thread t1(fn);
std::thread t2(fn);
std::thread t3(fn);
t1.join();
t2.join();
t3.join();
}
int main() {
get_id_test();
display_hardware_concurrency();
thread_local_sample();
}