-
Notifications
You must be signed in to change notification settings - Fork 1
/
ProducerConsumerBase.h
195 lines (176 loc) · 5.72 KB
/
ProducerConsumerBase.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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/**
* @file ProducerConsumerBase.h
*
* @brief ProducerConsumerBase base class for Producer and Consumer.
*
* @author Hovsep Papoyan
* Contact: papoyanhovsep93@gmail.com
* @Date 2024-01-10
*
*/
#ifndef PRODUCER_CONSUMER_BASE_H
#define PRODUCER_CONSUMER_BASE_H
#include "ThreadSafeSTLAdapter.h"
#include <iostream>
#include <queue>
namespace mt
{
template<typename Adapter>
class ProducerConsumerBase
{
protected:
static constexpr std::string_view Names[] = { "PRODUCER", "CONSUMER" };
enum class Command : unsigned char
{
EnableWorkerThread,
DisableWorkerThread,
ShutdownMainThread
};
enum class Type : unsigned char
{
Producer,
Consumer
};
private:
decltype(createThreadSafeSTLAdapterFrom(std::queue<Command>{})) m_commandQueue;
std::unique_ptr<std::jthread> m_mainThread;
protected:
Adapter& m_sharedContainer;
std::unique_ptr<std::jthread> m_workerThread;
std::mutex m_workerThreadMutex;
std::atomic<bool> m_workerThreadEnabled;
std::string_view m_name;
public:
explicit ProducerConsumerBase(const Type type, Adapter& sharedContainer);
ProducerConsumerBase(const ProducerConsumerBase&) = default;
ProducerConsumerBase(ProducerConsumerBase&&) = default;
ProducerConsumerBase& operator=(const ProducerConsumerBase&) = default;
ProducerConsumerBase& operator=(ProducerConsumerBase&) = default;
virtual ~ProducerConsumerBase() = default;
void enableWorkerThread();
void disableWorkerThread();
protected:
void runMainThread();
void shutdownMainThread();
private:
virtual void workerThreadWork() = 0;
void interruptWorkerThread();
void mainThreadWork();
};
template<typename Adapter>
ProducerConsumerBase<Adapter>::ProducerConsumerBase(const Type type, Adapter& sharedContainer)
: m_commandQueue(createThreadSafeSTLAdapterFrom(std::queue<Command>{}))
, m_sharedContainer(sharedContainer)
, m_workerThreadEnabled(false)
, m_name(Names[static_cast<unsigned char>(type)])
{ }
template<typename Adapter>
void ProducerConsumerBase<Adapter>::enableWorkerThread()
{
m_commandQueue.pushAndNotify(Command::EnableWorkerThread);
}
template<typename Adapter>
void ProducerConsumerBase<Adapter>::disableWorkerThread()
{
m_commandQueue.pushAndNotify(Command::DisableWorkerThread);
}
template<typename Adapter>
void ProducerConsumerBase<Adapter>::runMainThread()
{
try
{
m_mainThread = std::make_unique<std::jthread>([&] { mainThreadWork(); });
}
catch (const std::exception& ex)
{
std::cerr << m_name << " -> " << ex.what() << std::endl;
}
catch (...)
{
std::cerr << m_name << " -> Unknown exception" << std::endl;
}
}
template<typename Adapter>
void ProducerConsumerBase<Adapter>::shutdownMainThread()
{
try
{
m_commandQueue.pushAndNotify(Command::ShutdownMainThread);
}
catch (const std::exception& ex)
{
std::cerr << m_name << " -> " << ex.what() << std::endl;
}
catch (...)
{
std::cerr << m_name << " -> Unknown exception" << std::endl;
}
if (m_mainThread->joinable())
{
m_mainThread->join();
}
}
template<typename Adapter>
void ProducerConsumerBase<Adapter>::interruptWorkerThread()
{
std::lock_guard<std::mutex> lock(m_workerThreadMutex);
m_workerThreadEnabled = false;
if (m_workerThread->joinable())
{
m_workerThread->join();
}
}
template<typename Adapter>
void ProducerConsumerBase<Adapter>::mainThreadWork()
{
try
{
while (true)
{
Command currentCommand;
m_commandQueue.waitAndPop(currentCommand);
if (currentCommand == Command::EnableWorkerThread)
{
if (!m_workerThreadEnabled)
{
std::lock_guard<std::mutex> lock(m_workerThreadMutex);
m_workerThreadEnabled = true;
m_workerThread = std::make_unique<std::jthread>([&]
{
try
{
workerThreadWork();
}
catch (const std::exception& ex)
{
std::cerr << m_name << " -> " << ex.what() << std::endl;
}
catch (...)
{
std::cerr << m_name << " -> Unknown exception" << std::endl;
}
});
}
}
else if (currentCommand == Command::DisableWorkerThread)
{
interruptWorkerThread();
}
else if (currentCommand == Command::ShutdownMainThread)
{
interruptWorkerThread();
break;
}
}
}
catch (const std::exception& ex)
{
std::cerr << m_name << " -> " << ex.what() << std::endl;
}
catch (...)
{
std::cerr << m_name << " -> Unknown exception" << std::endl;
}
}
} // namespace mt
#endif