51# ifndef Q_CONSUMER_THREADS_H
52# define Q_CONSUMER_THREADS_H
55# include <condition_variable>
69class [[deprecated(
"ConsumerQueueEvent goes in 8.0.0: use ThreadPool (thread_pool.H)")]]
72 enum class Status { Ready, Executing };
74 std::atomic<Status> status = { Status::Ready };
88 virtual bool run() = 0;
101class [[deprecated(
"QueueTheadsPool goes in 8.0.0: use ThreadPool (thread_pool.H)")]]
146 size_t num_threads = 0;
147 size_t num_active = 0;
148 bool suspended =
true;
149 bool shutting_down =
false;
150 bool shutdown_done =
false;
188 std::unique_lock<std::mutex> critical_section(lck);
194 <<
"Thread creation with an event owned by another";
201 std::thread(handler,
this, event).detach();
206 event_list.
remove_ne([event] (
auto e) {
return e == event; });
215 std::unique_lock<std::mutex> critical_section(lck);
216 if (num_threads == 0)
220 q.
put(std::forward<T>(item));
234 std::unique_lock<std::mutex> critical_section(lck);
244 std::unique_lock<std::mutex> critical_section(lck);
254 std::unique_lock<std::mutex> critical_section(lck);
260 std::unique_lock<std::mutex> critical_section(lck);
261 return shutdown_done;
271 std::unique_lock<std::mutex> critical_section(lck);
272 if (not shutting_down)
274 shutting_down =
true;
279 if (running_pool ==
this)
282 shutdown_cv.wait(critical_section, [
this] {
return num_threads == 0; } );
283 shutdown_done =
true;
292 std::unique_lock<std::mutex> critical_section(
lck);
298 cond.wait(critical_section,
308 event->item =
q.get();
310 critical_section.unlock();
318 critical_section.lock();
324 critical_section.lock();
Exception handling system with formatted messages for Aleph-w.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
Dynamic queue of elements of generic type T based on single linked list.
T & put(const T &data)
The type of element.
Doubly-linked list (defined in tpl_dynList.H).
T & append(const T &item)
T remove_ne() noexcept(std::is_nothrow_move_constructible_v< T >)
Remove the first item of the list, which must not be empty.
An event of a queue: what its derived classes do with each item.
virtual bool run()=0
this is a item extracted from the queue
virtual ~ConsumerQueueEvent()=default
Virtual, so that a derived event is destroyed through this class.
bool exists(Operation &op) const
Test for existence in the container of an element satisfying a criterion.
virtual ~ConsumerQueueEvent()
size_t get_count() const noexcept
virtual void run()=0
this is a item extracted from the queue
void increment_count() noexcept
Pool of consumer threads form a queue.
static thread_local const QueueTheadsPool * running_pool
~QueueTheadsPool()
Shut the pool down, as shutdown() does, if it was not, and wait for the events that are running: it t...
void shutdown()
Make the threads finish, also those of a suspended pool, and wait for them; again,...
std::condition_variable cond
DynList< ConsumerQueueEvent * > event_list
static void handler(QueueTheadsPool< T > *threads_pool, ConsumerQueueEvent *event)
std::condition_variable shutdown_cv
void run_event(ConsumerQueueEvent *event)
void create_thread(ConsumerQueueEvent *event)
Create a thread that consumes the items of the queue with event.
std::decay_t< typename HeadC::Item_Type > T
and
Check uniqueness with explicit hash + equality functors.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Dynamic queue implementation based on linked lists.