forked from vic4key/Vutils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSample.ThreadPool.h
More file actions
80 lines (57 loc) · 1.43 KB
/
Sample.ThreadPool.h
File metadata and controls
80 lines (57 loc) · 1.43 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
#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::CScopeStopWatch logger(_T("ThreadPool =>"), vu::CScopeStopWatch::Console);
// Single-threading
logger.Reset();
for (int i = 0; i < 10; i++)
{
fn();
}
logger.Log(_T("Taken : "));
// Default Multi-threading
logger.Reset();
vu::CThreadPool pool;
for (int i = 0; i < 10; i++)
{
pool.AddTask(fn);
}
pool.Launch();
logger.Log(_T("Taken : "));
// STL Multi-threading
class CSampleTask : public vu::CSTLThreadT<std::vector<int>>
{
public:
CSampleTask(std::vector<int>& items) : CSTLThreadT(items)
{
m_results.resize(this->Iterations());
};
int Result()
{
return std::accumulate(m_results.cbegin(), m_results.cend(), 0);
}
virtual const vu::eReturn 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::eReturn::Ok;
}
private:
std::vector<int> m_results;
};
logger.Reset();
std::vector<int> items(10);
std::iota(items.begin(), items.end(), 0);
CSampleTask task(items);
task.Launch();
std::cout << "Result is " << task.Result() << std::endl;
logger.Log(_T("Taken : "));
return vu::VU_OK;
}