-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreads.cpp
More file actions
65 lines (56 loc) · 1.96 KB
/
Threads.cpp
File metadata and controls
65 lines (56 loc) · 1.96 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
64
65
/**
* \file Threads.cpp
* \brief Create std::vector of threads
*/
#include <StdStream/StdStream.h>
#include <StdTest/StdTest.h>
#include <Stl.h>
//--------------------------------------------------------------------------------------------------
void
worker(
const int value
)
{
std::this_thread::sleep_for( std::chrono::seconds(value) );
std::cout << "[worker] running on thread " << std::this_thread::get_id() << std::endl;
}
//--------------------------------------------------------------------------------------------------
int main(int, char **)
{
const int threadsNum = std::thread::hardware_concurrency();
std::cout << STD_TRACE_VAR(threadsNum) << std::endl;
std::vector<std::thread> pool;
for (int i = 0; i < threadsNum; ++ i) {
pool.emplace_back( std::thread(worker, i) );
}
for (auto &it_t : pool){
it_t.join();
}
return EXIT_SUCCESS;
}
//--------------------------------------------------------------------------------------------------
#if OUTPUT
---------------------------------------------
threadsNum: 20
[worker] running on thread 133090835957312
[worker] running on thread 133090825471552
[worker] running on thread 133090814985792
[worker] running on thread 133090670282304
[worker] running on thread 133090804500032
[worker] running on thread 133090794014272
[worker] running on thread 133090783528512
[worker] running on thread 133090699642432
[worker] running on thread 133090689156672
[worker] running on thread 133090659796544
[worker] running on thread 133090649310784
[worker] running on thread 133090638825024
[worker] running on thread 133090628339264
[worker] running on thread 133090617853504
[worker] running on thread 133090607367744
[worker] running on thread 133090596881984
[worker] running on thread 133090586396224
[worker] running on thread 133090575910464
[worker] running on thread 133090565424704
[worker] running on thread 133090554938944
---------------------------------------------
#endif