-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain.cpp
More file actions
149 lines (134 loc) · 5.04 KB
/
Copy pathmain.cpp
File metadata and controls
149 lines (134 loc) · 5.04 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/***********************************************************************************************************************
* @file main.cpp
*
* @brief Test code for the C++11 Thread Pool of Atanas Rusev and Ferai Ali
*
* @details
*
*
* The code is based completely on C++11 features. The purpose is to be able to integrate it
* in older projects which have not yet reached C++14 or higher. If you need newer features
* fork the code and get it to the next level yourself.
*
* @author Atanas Rusev and Ferai Ali
*
* @copyright 2019 Atanas Rusev and Ferai Ali, MIT License. Check the License.h file in the library.
*
***********************************************************************************************************************/
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <chrono>
#include "thread_pool.h"
using namespace std::chrono_literals;
using namespace std;
void print(std::string text)
{
std::cout << text << std::endl;
}
/***********************************************************************************************************************
* @brief A function to test the thread pool with longer tasks
*
* @details Based on lambdas we add 3000 jobs that are with default priority and 3000 Critical jobs.
* Then we add a sleep for the main thread that will wait until the jobs are executed for sure.
*
* @pre Thread pool creation
* @post
* @param[in] CTP::ThreadPool &thread_pool
* @return None
*
* @author Atanas Rusev and Ferai Ali
*
* @copyright 2019 Atanas Rusev and Ferai Ali, MIT License. Check the License file in the library.
*
***********************************************************************************************************************/
void run_long_tasks(CTP::ThreadPool &thread_pool)
{
for (int i = 0; i < 3000; i++)
{
// Schedule a lambda that will first wait 10 milliseconds and then print "Hello World i : ",
// where i is the for cycle index
thread_pool.Schedule([i]()
{
const auto start = std::chrono::system_clock::now();
while (start + 10ms > std::chrono::system_clock::now()) {}
std::cout << "NORM: " << i << std::endl;
});
// Schedule a lambda that will first wait 10 milliseconds and then print "First ! i", where
// i is the for cycle index
thread_pool.Schedule(CTP::Priority::Critical, [i]()
{
const auto start = std::chrono::system_clock::now();
while (start + 10ms > std::chrono::system_clock::now()) {}
std::cout << "CRIT: " << i << std::endl;
});
}
// simply block the main thread to wait a bit
std::this_thread::sleep_for(15s);
}
/***********************************************************************************************************************
* @brief A function to test the thread pool with shorter tasks
*
* @details Based on lambdas we add few jobs that are with default priority.
* Then we add a sleep for the main thread that will wait until the jobs are executed for sure.
*
* @pre Thread pool creation
* @post
* @param[in] CTP::ThreadPool &thread_pool
* @return None
*
* @author Atanas Rusev and Ferai Ali
*
* @copyright 2019 Atanas Rusev and Ferai Ali, MIT License. Check the License file in the library.
*
***********************************************************************************************************************/
void run_small_tasks(CTP::ThreadPool &thread_pool)
{
for (int i = 0; i < 2; i++)
{
thread_pool.Schedule([i]()
{
const auto start = std::chrono::system_clock::now();
while (start + 2000ms > std::chrono::system_clock::now()) {}
std::cout << "MINI: " << i << std::endl;
});
}
std::this_thread::sleep_for(2s);
}
/***********************************************************************************************************************
* @brief Main that creates a thread pool and tests it.
*
* @details Tested with default number of threads (the return of std::thread::hardware_concurrency). Tested also
* with 1, 4 and 40 threads. Test Machine - HP ZBook 15 G5 with 16 GB RAM and Intel® Core™ i7-8750H Processor.
* Built initially with Microsoft Visual Studio 2015 as a console application.
*
* @author Atanas Rusev and Ferai Ali
*
* @copyright 2019 Atanas Rusev and Ferai Ali, MIT License. Check the License file in the library.
*
***********************************************************************************************************************/
int main()
{
CTP::ThreadPool thread_pool;
// CTP::ThreadPool thread_pool(1);
// CTP::ThreadPool thread_pool(4);
// CTP::ThreadPool thread_pool(40);
// example with a short lambda from here:
auto resultOf34 = thread_pool.Schedule([]()
{
int i = 3 * 4;
print(std::to_string(i));
return i;
});
// example for waiting on a future
if (resultOf34.wait_for(0ms) == std::future_status::ready)
{
auto res = resultOf34.get();
}
auto res = resultOf34.get();
for(int i = 0; i < 2; i++) run_long_tasks(thread_pool);
for (int i = 0; i < 2; i++) run_small_tasks(thread_pool);
std::this_thread::sleep_for(5s);
return 0;
}