-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_threadpool.cpp
60 lines (50 loc) · 1.37 KB
/
test_threadpool.cpp
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
#include "threadpool.h"
#include <unistd.h>
#include <iostream>
#include <thread>
#include <future>
#include <chrono>
using namespace std::chrono;
static bool thread_pool_init_func = []()
{
InitializeThreadPool(4);
return true;
}();
int main()
{
for (int i = 0; i < 8; i++)
{
GetThreadPool()->enqueue(0, [i](int val) {
std::cout << val << " start" << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
std::cout << val << " end" << std::endl;
}, i);
}
std::vector<int> future_vector;
for (int i = 0; i < 10; i++)
{
std::promise<int> pr;
std::future<int> fu = pr.get_future();
GetThreadPool()->enqueue(0, [i](std::promise<int>& pr) {
std::this_thread::sleep_for(std::chrono::milliseconds(200));
pr.set_value(i);
}, std::ref(pr));
future_vector.push_back(fu.get());
}
std::cout << "Future vector: ";
for (int val : future_vector)
{
std::cout << val << " ";
}
std::cout << std::endl;
auto fu = GetThreadPool()->enqueue(0, []() -> int {
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
return 100;
});
std::cout << "Result returned by direct future assignment: " << fu.get() << std::endl;
while (true)
{
pause();
}
return 0;
}