1 #ifndef MARLINMT_CONCURRENCY_WORKER_h 2 #define MARLINMT_CONCURRENCY_WORKER_h 1 14 #include <condition_variable> 22 namespace concurrency {
24 template <
typename IN,
typename OUT>
26 template <
typename IN,
typename OUT>
36 template <
typename IN,
typename OUT>
51 virtual OUT
process( IN && data ) = 0 ;
65 template <
typename OUT>
77 template <
typename IN>
82 virtual void process( IN && data ) = 0 ;
101 template <
typename IN,
typename OUT>
106 template <
typename IN>
112 template <
typename OUT>
129 template <
typename IN,
typename OUT>
151 Worker(
Pool &pool, std::unique_ptr<IMPL> impl ) ;
172 bool running()
const ;
177 bool waiting()
const ;
188 std::thread _thread {} ;
190 std::atomic<bool> _stopFlag {
false} ;
192 std::atomic<bool> _waitingFlag {
false} ;
194 std::unique_ptr<Impl> _impl {
nullptr} ;
206 namespace concurrency {
208 template <
typename IN,
typename OUT>
209 template <
typename IMPL,
class>
212 _impl(
std::move(impl)) {
218 template <
typename IN,
typename OUT>
225 template <
typename IN,
typename OUT>
232 _impl->processElement( element ) ;
256 template <
typename IN,
typename OUT>
263 template <
typename IN,
typename OUT>
265 return (
_thread.get_id() != std::thread::id() ) ;
270 template <
typename IN,
typename OUT>
277 template <
typename IN,
typename OUT>
std::unique_ptr< Impl > _impl
virtual OUT process(IN &&data)=0
Process a single queued data taken form the thread pool.
std::atomic< bool > _stopFlag
Whether the worker thread is waiting for data.
void setValue(OUT &&output)
Set the value to be retrieved in the future variable.
bool running() const
Whether the worker thread is currently running.
std::atomic< bool > _isDone
The thread pool stop flag.
constexpr unsigned long long value(const Flag_t &flag)
void processElement(QueueElement< IN, OUT > &element)
Process queued element from the thread pool.
WorkerBase class Base class to implement processing of task data (so called queued-element) pushed in...
void run()
The method executing in the worker thread.
std::thread _thread
The stop flag.
void setValue(OUT &&output)
Pool & _threadPool
< The parent thread pool
std::condition_variable _conditionVariable
The actual thread pool.
ThreadPool class The template parameter T is the type of data to enqueue and process in worker thread...
std::mutex _mutex
< The synchronization mutex
bool waiting() const
Whether the worker is waiting.
void start()
Start the worker thread.
QueueType _queue
Runtime flag...
void stop()
Switch ON the stop flag, requesting the worker thread to stop.
bool pop(T &value)
Pop and get the front element in the queue.
PEPScheduler::InputType Input
virtual ~WorkerBase()=default
WorkerOutput struct Stores the output of a processor sequence call.
IN takeInput()
Take the input data.
std::atomic< bool > _waitingFlag
The worker implementation.
QueueElement class A template queue element used in the thread pool.
void join()
Join the worker thread.