cppthread 1.1.16
C++ Thread Library
worker.h
Go to the documentation of this file.
1// Copyright (c) 2013-2025 Made to Order Software Corp. All Rights Reserved
2//
3// https://snapwebsites.org/project/cppthread
4// contact@m2osw.com
5//
6// This program is free software; you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation; either version 2 of the License, or
9// (at your option) any later version.
10//
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15//
16// You should have received a copy of the GNU General Public License along
17// with this program; if not, write to the Free Software Foundation, Inc.,
18// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19#pragma once
20
27// self
28//
29#include <cppthread/exception.h>
30#include <cppthread/fifo.h>
31#include <cppthread/runner.h>
32
33
34
35namespace cppthread
36{
37
38
39
40template<class T>
41class worker
42 : public runner
43{
44public:
45 typedef T work_load_type;
46
48 std::string const & name
49 , std::size_t position
50 , typename fifo<T>::pointer_t in
51 , typename fifo<T>::pointer_t out)
52 : runner(name)
53 , f_in(in)
54 , f_out(out)
56 {
57 if(f_in == nullptr)
58 {
59 throw invalid_error("a worker object must be given a valid input FIFO");
60 }
61 }
62
63 worker(worker const & rhs) = delete;
64 worker<T> & operator = (worker<T> const & rhs) = delete;
65
66 std::size_t position() const
67 {
68 return f_position;
69 }
70
71 bool is_working() const
72 {
73 guard lock(f_mutex);
74 return f_working;
75 }
76
77 size_t runs() const
78 {
79 guard lock(f_mutex);
80 return f_runs;
81 }
82
83 virtual void run()
84 {
85 // on a re-run, f_working could be true
86 {
87 guard lock(f_mutex);
88 f_working = false;
89 }
90
91 while(continue_running())
92 {
93 if(f_in->pop_front(f_payload, -1))
94 {
96 {
97 {
98 guard lock(f_mutex);
99 f_working = true;
100 ++f_runs;
101 }
102
103 // note: if do_work() throws, then f_working remains
104 // set to 'true' which should not matter
105 //
106 if(do_work())
107 {
108 if(f_out != nullptr)
109 {
110 f_out->push_back(f_payload);
111 }
112 }
113
114 {
115 guard lock(f_mutex);
116 f_working = false;
117 }
118 }
119 }
120 else
121 {
122 // if the FIFO is empty and it is marked as done, we
123 // want to exit immediately
124 //
125 if(f_in->is_done())
126 {
127 break;
128 }
129 }
130 }
131 }
132
133 virtual bool do_work() = 0;
134
135protected:
136 T f_payload = T();
139
140private:
141 std::size_t const f_position;
142 bool f_working = false;
143 std::size_t f_runs = 0;
144};
145
146
147
148} // namespace cppthread
149// vim: ts=4 sw=4 et
std::shared_ptr< fifo_type > pointer_t
A smart pointer to the FIFO.
Definition fifo.h:170
Lock a mutex in an RAII manner.
Definition guard.h:42
The runner is the class that wraps the actual system thread.
Definition runner.h:65
virtual bool continue_running() const
Whether the thread should continue running.
Definition runner.cpp:153
mutex f_mutex
The mutex of this thread.
Definition runner.h:86
A runner augmentation allowing for worker threads.
Definition worker.h:43
fifo< T >::pointer_t f_in
The input fifo.
Definition worker.h:137
size_t runs() const
Number of time this worker got used.
Definition worker.h:77
std::size_t position() const
Get the worker thread position.
Definition worker.h:66
T f_payload
The payload this worker is processing.
Definition worker.h:136
std::size_t const f_position
The position of this worker in the pool.
Definition worker.h:141
worker< T > & operator=(worker< T > const &rhs)=delete
Deleted assignment operator.
worker(worker const &rhs)=delete
Deleted copy operator.
bool f_working
Whether this worker is currently working (true) or not (false).
Definition worker.h:142
T work_load_type
Type T of the worker.
Definition worker.h:45
fifo< T >::pointer_t f_out
The output fifo.
Definition worker.h:138
worker(std::string const &name, std::size_t position, typename fifo< T >::pointer_t in, typename fifo< T >::pointer_t out)
Initialize a worker thread.
Definition worker.h:47
bool is_working() const
Check whether this specific worker thread is busy.
Definition worker.h:71
std::size_t f_runs
The number of times this worker ran.
Definition worker.h:143
virtual void run()
Implement the worker loop.
Definition worker.h:83
virtual bool do_work()=0
Worker Function.
Exceptions for the thread environment.
Thread Runner and Managers.
Thread Runner and Managers.

This document is part of the Snap! Websites Project.

Copyright by Made to Order Software Corp.