-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_thread_pool.cpp
100 lines (79 loc) · 2.41 KB
/
test_thread_pool.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
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
#include <cppcoro/static_thread_pool.hpp>
#include <cppcoro/task.hpp>
#include <cppcoro/sync_wait.hpp>
#include <cppcoro/when_all.hpp>
#include <cppcoro/schedule_on.hpp>
#include <iostream>
#include <vector>
#include "async_channel.hpp"
#include "measure.h"
/*
* test/static_thread_pool_tests.cpp
*/
int main(int argc, char *argv[])
{
int numTask = 10;
if(argc > 1)
numTask = atoi(argv[1]);
else
std::cout << "You can pass in a command line argument as the number of producers/consumers to generate. Default to 10.\n";
//cppcoro::static_thread_pool thread_pool{4};
cppcoro::static_thread_pool thread_pool;
AsyncChannel<int> chan(10, &thread_pool);
std::mutex display_mutex;
const int test_iter = 1;
auto makeTask = [&]() -> cppcoro::task<> {
std::unique_lock<std::mutex> lck(display_mutex);
std::cout << "[INFO] one task\n";
lck.unlock();
co_await thread_pool.schedule();
lck.lock();
std::cout << "[INFO] one task done\n";
lck.unlock();
};
auto MakeProducer = [&](int id) -> cppcoro::task<> {
std::unique_lock<std::mutex> lck(display_mutex);
std::cout << "[INFO] producer " << id << " \n";
lck.unlock();
co_await thread_pool.schedule();
co_await chan.send(id);
// co_await chan.recv_wait_queue_.GetDataDog(id);
lck.lock();
std::cout << "[INFO] producer " << id << " done\n";
lck.unlock();
};
auto MakeConsumer = [&](int id) -> cppcoro::task<> {
std::unique_lock<std::mutex> lck(display_mutex);
std::cout << "[INFO] consumer " << id << " \n";
lck.unlock();
co_await thread_pool.schedule();
int val = co_await chan.recv();
// int val = co_await chan.send_wait_queue_.GetDataDog();
lck.lock();
std::cout << "[INFO] consumer " << id << " get data " << val << " done\n";
lck.unlock();
};
Measure m1;
//for (int k = 0; k < test_iter; ++k) {
// std::cout << "[ITER] " << k << " iter\n";
std::vector<cppcoro::task<>> tasks;
for (int i = 0; i < numTask; ++i) {
//tasks.push_back(makeTask());
//tasks.push_back(MakeProducer(i));
//tasks.push_back(MakeConsumer(i));
tasks.push_back(MakeConsumer(i));
tasks.push_back(MakeProducer(i));
}
/*
for (int i = 0; i < 100; ++i) {
//tasks.push_back(makeTask());
tasks.push_back(MakeProducer(i));
//tasks.push_back(MakeConsumer(i));
}
*/
cppcoro::sync_wait(cppcoro::when_all(std::move(tasks)));
//}
Measure m2;
std::cout << m2.diff_time_millisec(m1).count() / test_iter << "msec\n";
return 0;
}