-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorker.h
More file actions
179 lines (157 loc) · 4.73 KB
/
Copy pathWorker.h
File metadata and controls
179 lines (157 loc) · 4.73 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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/**
ThreadWorker (Worker.h)
A simple example of a class that encapsulates a thread and a worker which
works within that thread.
See the blog article at https://codesmith.hashnode.dev/ for more info
on this tutorial.
By far this is NOT perfect. As all persons we all are learning new things
I want to share everything I have learned in the past to anyone
willing to come to the industry or learn these things just for hobby!
For an example, please see the provided example in
ThreadWorkerDemo.cpp
You can pass any task with a lambda to a free worker as showed.
I love programming, I work in the field and it is my hobby!
I hope you will contact me.
If you find this thing useful, I would not mind of you to get in to
contact with a fellow and old time programmer <3
LICENSE - MIT
Copyright (c) 2012-2021 Erno Pakarinen (codesmith-fi@gmail.com)
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.
*/
#ifndef __WORKER_H_DEFINED__
#define __WORKER_H_DEFINED__
#include <cstdint>
#include <chrono>
#include <thread>
#include <string>
#include <mutex>
#include <functional>
#include "DebugLogger.h"
class Worker
{
public: // enums
enum class WorkerState : uint16_t {
ECreated,
EIdle,
EWorking,
EExiting
};
static std::string WorkerStateToString(const WorkerState& _state) {
std::string statestr;
switch(_state) {
case WorkerState::EIdle:
{
statestr = "Idle";
break;
}
case WorkerState::EWorking:
{
statestr = "Working";
break;
}
case WorkerState::EExiting:
{
statestr = "Exiting";
break;
}
default:
{
statestr = "Created";
break;
}
}
return statestr;
}
static const uint16_t KThreadWaitSeconds = 1;
public:
Worker() : Worker(0) { };
Worker(uint16_t _id) : m_thread{}, m_state(WorkerState::ECreated),
m_id(_id), m_running(false), m_usefunction(false) {
LOG_INFO() << "Created Worker number: " << m_id;
};
virtual ~Worker() {
LOG_INFO() << "Destroyed Worker number: " << m_id;
};
public:
std::uint16_t id() const { return m_id; };
const WorkerState& state() const { return m_state; }
void start() {
LOG_INFO() << "Starting worker: " << m_id;
m_running = true;
m_state = WorkerState::EIdle;
m_thread = std::thread([this]() { doRun(); });
};
void stop() {
LOG_INFO() << "Stopping worker: " << m_id;
if (m_running) {
m_running = false;
m_state = WorkerState::EExiting;
m_thread.join();
}
};
void work() {
const std::lock_guard<std::mutex> lock(m_mutex);
if (m_state == WorkerState::EIdle) {
m_usefunction = false;
m_state = WorkerState::EWorking;
}
};
void work(std::function<void(void)> task) {
const std::lock_guard<std::mutex> lock(m_mutex);
if (m_state == WorkerState::EIdle) {
m_task = task;
m_usefunction = true;
m_state = WorkerState::EWorking;
}
}
protected:
virtual void doWork() {
LOG_INFO() << "Default worker: " << m_id << " work ended\n";
}
private:
void doRun() {
while(m_running) {
auto timep1 = std::chrono::system_clock::now();
if (m_state == WorkerState::EWorking) {
LOG_INFO() << "Worker " << m_id << " starting task\n";
if(m_usefunction) {
m_task();
} else {
doWork();
}
m_state = WorkerState::EIdle;
auto timep2 = std::chrono::system_clock::now();
std::chrono::duration<float> elapsed(timep2 - timep1);
LOG_INFO() << "Worker [" << m_id << "] task complete, time spent: " << std::to_string(elapsed.count());
}
else {
LOG_INFO() << "Worker: " << m_id << " @" << Worker::WorkerStateToString(m_state);
std::this_thread::sleep_for(std::chrono::seconds(KThreadWaitSeconds));
}
}
}
private:
std::thread m_thread;
std::mutex m_mutex;
std::function<void(void)> m_task;
WorkerState m_state;
std::uint16_t m_id;
bool m_running;
bool m_usefunction;
};
#endif // __WORKER_H_DEFINED__