OpenJPH: /build/openjph/src/OpenJPH/src/apps/others/ojph_threads.cpp Source File

OpenJPH Open-source implementation of JPEG2000 Part-15

Loading...
Searching...
No Matches
ojph_threads.cpp
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#include "ojph_threads.h"
39
40namespace ojph
41{
42namespace thds
43{
44
46//
47//
48//
49//
50//
52
54thread_pool::~thread_pool()
55{
56 stop.store(true, std::memory_order_release);
57 condition.notify_all();
58 for (size_t i = 0; i < threads.size(); ++i)
59 threads[i].join();
60}
61
63 void thread_pool::init(size_t num_threads)
64{
65 if (threads.size() < num_threads)
66 threads.resize(num_threads);
67
68 for (size_t i = 0; i < num_threads; ++i)
69 threads[i] = std::thread(start_thread, this);
70}
71
73 void thread_pool::add_task(worker_thread_base* task)
74{
75 mutex.lock();
76 tasks.push_back(task);
77 condition.notify_one();
78 mutex.unlock();
79}
80
82 void thread_pool::start_thread(thread_pool* tp)
83{
84 while (1)
85 {
86 // setup the condition variable
87 std::unique_lock<std::mutex> lock(tp->mutex);
88 // wait releases the mutex, blocks until notified (or spuriously),
89 // and acquire the mutex
90 tp->condition.wait(lock);
91
92 if(tp->stop.load(std::memory_order_acquire))
93 return;
94
95 worker_thread_base* task = NULL;
96 if (!tp->tasks.empty())
97 {
98 task = tp->tasks.front();
99 tp->tasks.pop_front();
100 }
101 lock.unlock();
102 if (task)
103 task->execute();
104 }
105}
106
107} // !thds namespace
108} // !ojph namespace
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::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
Definition ojph_threads.h:51
ojph
Definition ojph_img_io.h:52
ojph_threads.h