-
Notifications
You must be signed in to change notification settings - Fork 24
/
Sample.ThreadPool.h
80 lines (57 loc) · 1.52 KB
/
Sample.ThreadPool.h
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
#pragma once
#include "Sample.h"
DEF_SAMPLE(ThreadPool)
{
const auto fn = []()
{
std::this_thread::sleep_for(std::chrono::seconds(1));
std::cout << VU_FUNC_NAME << std::endl;
};
vu::ScopeStopWatch logger(ts("ThreadPool =>"), ts(""), vu::ScopeStopWatch::console);
// Single-threading
logger.reset();
for (int i = 0; i < 10; i++)
{
fn();
}
logger.log(ts("Taken : "));
// Default Multi-threading
logger.reset();
vu::ThreadPool pool;
for (int i = 0; i < 10; i++)
{
pool.add_task(fn);
}
pool.launch();
logger.log(ts("Taken : "));
// STL Multi-threading
class SampleTask : public vu::STLThreadT<std::vector<int>>
{
public:
SampleTask(std::vector<int>& items) : STLThreadT(items)
{
m_results.resize(this->iterations());
};
int result()
{
return std::accumulate(m_results.cbegin(), m_results.cend(), 0);
}
virtual vu::return_type task(int& item, int iteration, int threadid)
{
std::this_thread::sleep_for(std::chrono::seconds(1));
std::cout << VU_FUNC_NAME << std::endl;
m_results[iteration] += item;
return vu::return_type::Ok;
}
private:
std::vector<int> m_results;
};
logger.reset();
std::vector<int> items(10);
std::iota(items.begin(), items.end(), 0);
SampleTask task(items);
task.launch();
std::cout << "result is " << task.result() << std::endl;
logger.log(ts("Taken : "));
return vu::VU_OK;
}