-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
114 lines (90 loc) · 3.76 KB
/
main.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
* Copyright (C) 2020 Conrad Hübler <Conrad.Huebler@gmx.net>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
// #define _CxxThreadPool_Verbose
#define _CxxThreadPool_BarWidth 100
#include "include/CxxThreadPool.h"
#include "include/Timer.h"
#include <iostream>
using namespace std;
class Thread : public CxxThread{
public:
Thread(int rand) : m_rand(rand)
{
}
~Thread() = default;
inline int execute()
{
//printf("Thread %d started ...\n", m_rand);
std::this_thread::sleep_for(std::chrono::milliseconds(m_rand));
//printf("Thread finished! ... %d ... \n", m_rand);
return 0;
}
private:
int m_rand;
};
int main()
{
unsigned int seed = time(NULL);
RunTimer timer(true);
int max_threads = 1000;
int active_threads = 32;
CxxThreadPool *pool = new CxxThreadPool;
pool->setActiveThreadCount(active_threads);
pool->setProgressBar(CxxThreadPool::ProgressBarType::Discrete);
//pool->EcoBar(true);
std::cout << "This a example application to present the CxxThreadPool method!\nThe demo will run with " << max_threads << " jobs and " << active_threads << " active threads!\n";
std::cout << "Each thread will be initialised with a random number : rand_r(&seed)/1e6 - that equals the msecs to sleep.\n";
std::cout << "The thread pool will run a couple of times - with the same threads and random numbers to demonstrate the :\nSingle Pool,\nStatic Pool and\nDynamic Pool ability of the CxxThreadPool Class.\n\n";
for(int i = 0; i < max_threads; ++i)
{
Thread *thread = new Thread(rand_r(&seed)/1e6);
pool->addThread(thread);
}
std::cout << "Single Pool: Each thread will be run isolated, so " << max_threads << " will be executed." << std::endl;
pool->StartAndWait();
pool->Reset();
std::cout << "Static Pool: " << max_threads / active_threads << " threads will be executed ( + remaining individual threads )." << std::endl;
pool->StaticPool();
pool->StartAndWait();
pool->Reset();
std::cout << "Dynamic Pool with divider 1: " << max_threads / active_threads << " threads will be executed ( + remaining individual threads )." << std::endl;
pool->DynamicPool(1);
pool->StartAndWait();
pool->Reset();
std::cout << "Dynamic Pool with divider 2." << std::endl;
pool->DynamicPool();
pool->StartAndWait();
pool->Reset();
std::cout << "Dynamic Pool with divider 3." << std::endl;
pool->DynamicPool(3);
pool->StartAndWait();
pool->Reset();
std::cout << "Dynamic Pool with divider 4." << std::endl;
pool->DynamicPool(4);
pool->StartAndWait();
pool->Reset();
std::cout << "Dynamic Pool with divider 5." << std::endl;
pool->DynamicPool(4);
pool->StartAndWait();
return 0;
}