OpenJPH
Open-source implementation of JPEG2000 Part-15
Loading...
Searching...
No Matches
ojph_threads.h
Go to the documentation of this file.
1//***************************************************************************/
2// This software is released under the 2-Clause BSD license, included
3// below.
4//
5// Copyright (c) 2024, Aous Naman
6// Copyright (c) 2024, Kakadu Software Pty Ltd, Australia
7// Copyright (c) 2024, The University of New South Wales, Australia
8//
9// Redistribution and use in source and binary forms, with or without
10// modification, are permitted provided that the following conditions are
11// met:
12//
13// 1. Redistributions of source code must retain the above copyright
14// notice, this list of conditions and the following disclaimer.
15//
16// 2. Redistributions in binary form must reproduce the above copyright
17// notice, this list of conditions and the following disclaimer in the
18// documentation and/or other materials provided with the distribution.
19//
20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
26// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31//***************************************************************************/
32// This file is part of the OpenJPH software implementation.
33// File: ojph_threads.h
34// Author: Aous Naman
35// Date: 22 April 2024
36//***************************************************************************/
37
38#ifndef OJPH_THREADS_H
39#define OJPH_THREADS_H
40
41#include <atomic>
42#include <vector>
43#include <thread>
44#include <mutex>
45#include <deque>
46#include <condition_variable>
47
48namespace ojph
49{
50namespace thds
51{
52
54//
55//
56//
57//
58//
60
61/*****************************************************************************/
69{
70public:
75 virtual ~worker_thread_base() { }
76
80 virtual void execute() = 0;
81};
82
83
85//
86//
87//
88//
89//
91
92/*****************************************************************************/
98{
99public:
103 thread_pool() { stop.store(false, std::memory_order_relaxed); }
107 ~thread_pool();
108
109public:
115 void init(size_t num_threads);
116
122 void add_task(worker_thread_base* task);
123
129 size_t get_num_threads() { return threads.size(); }
130
131private:
137 static void start_thread(thread_pool* tp);
138
139private:
140 std::vector<std::thread> threads;
141 std::deque<worker_thread_base*> tasks;
142 std::mutex mutex;
143 std::condition_variable condition;
144 std::atomic_bool stop;
145};
146
147} // !thds namespace
148} // !ojph namespace
149
150
151
152
153
154
155#endif // !OJPH_THREADS_H
Implements a pool of threads, and can queue tasks.
std::vector< std::thread > threads
std::atomic_bool stop
static void start_thread(thread_pool *tp)
A static function to start a thread.
std::condition_variable condition
~thread_pool()
default destructor
std::deque< worker_thread_base * > tasks
void add_task(worker_thread_base *task)
Adds a task to the thread pool.
void init(size_t num_threads)
Initializes the thread pool.
size_t get_num_threads()
Returns the number of threads in the thread pool.
thread_pool()
default constructor
A base object for queuing tasks in the thread_pool.
virtual void execute()=0
Derived functions must define this function to execute its work.
virtual ~worker_thread_base()
virtual construction is a necessity to deconstruct derived objects.