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
48 namespace ojph
49{
50 namespace thds
51{
52
54//
55//
56//
57//
58//
60
61/*****************************************************************************/
68 class worker_thread_base
69{
70 public:
75 virtual ~worker_thread_base() { }
76
80 virtual void execute() = 0;
81};
82
83
85//
86//
87//
88//
89//
91
92/*****************************************************************************/
97 class thread_pool
98{
99 public:
103 thread_pool() { stop.store(false, std::memory_order_relaxed); }
107 ~thread_pool();
108
109 public:
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
131 private:
137 static void start_thread(thread_pool* tp);
138
139 private:
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
ojph::thds::thread_pool
Implements a pool of threads, and can queue tasks.
Definition ojph_threads.h:98
ojph::thds::thread_pool::threads
std::vector< std::thread > threads
Definition ojph_threads.h:140
ojph::thds::thread_pool::stop
std::atomic_bool stop
Definition ojph_threads.h:144
ojph::thds::thread_pool::start_thread
static void start_thread(thread_pool *tp)
A static function to start a thread.
Definition ojph_threads.cpp:82
ojph::thds::thread_pool::mutex
std::mutex mutex
Definition ojph_threads.h:142
ojph::thds::thread_pool::condition
std::condition_variable condition
Definition ojph_threads.h:143
ojph::thds::thread_pool::~thread_pool
~thread_pool()
default destructor
Definition ojph_threads.cpp:54
ojph::thds::thread_pool::tasks
std::deque< worker_thread_base * > tasks
Definition ojph_threads.h:141
ojph::thds::thread_pool::add_task
void add_task(worker_thread_base *task)
Adds a task to the thread pool.
Definition ojph_threads.cpp:73
ojph::thds::thread_pool::init
void init(size_t num_threads)
Initializes the thread pool.
Definition ojph_threads.cpp:63
ojph::thds::thread_pool::get_num_threads
size_t get_num_threads()
Returns the number of threads in the thread pool.
Definition ojph_threads.h:129
ojph::thds::thread_pool::thread_pool
thread_pool()
default constructor
Definition ojph_threads.h:103
ojph::thds::worker_thread_base
A base object for queuing tasks in the thread_pool.
Definition ojph_threads.h:69
ojph::thds::worker_thread_base::execute
virtual void execute()=0
Derived functions must define this function to execute its work.
ojph::thds::worker_thread_base::~worker_thread_base
virtual ~worker_thread_base()
virtual construction is a necessity to deconstruct derived objects.
Definition ojph_threads.h:75
ojph::thds
Definition ojph_threads.h:51
ojph
Definition ojph_img_io.h:52