Ocean
Loading...
Searching...
No Matches
ThreadPool.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8#ifndef META_OCEAN_BASE_THREAD_POOL_H
9#define META_OCEAN_BASE_THREAD_POOL_H
10
11#include "ocean/base/Base.h"
12#include "ocean/base/Lock.h"
13#include "ocean/base/Signal.h"
15#include "ocean/base/Thread.h"
16
17#include <functional>
18#include <queue>
19
20namespace Ocean
21{
22
23/**
24 * This class implements a pool holding re-usable thread objects for individual use.
25 * This class is not a singleton, use ThreadPoolSingleton in case a singleton is sufficient.
26 * @see ThreadPoolSingleton, Scheduler, TaskQueue.
27 * @ingroup base
28 */
29class OCEAN_BASE_EXPORT ThreadPool : protected Thread
30{
31 public:
32
33 /**
34 * Definition of a caller function without return parameter.
35 */
36 using Function = std::function<void()>;
37
38 protected:
39
40 /**
41 * This class implements one thread of this thread pool.
42 */
43 class OCEAN_BASE_EXPORT PoolThread : protected Thread
44 {
45 public:
46
47 /**
48 * Creates a new thread object.
49 * @param name The thread name
50 */
51 explicit PoolThread(const std::string& name);
52
53 /**
54 * Destructs a thread object.
55 */
56 ~PoolThread() override;
57
58 /**
59 * Returns whether this thread is currently busy.
60 * @return True, if so
61 */
62 inline bool isBusy() const;
63
64 /**
65 * Sets the thread function.
66 * @param function The thread function that will be invoked in this thread
67 */
68 void invoke(Function&& function);
69
70 protected:
71
72 /**
73 * Thread run function.
74 */
75 void threadRun() override;
76
77 protected:
78
79 /// Internal signal handling the internal thread execution.
81
82 /// The function that is invoked in this thread.
84
85 /// Thread lock.
86 mutable Lock lock_;
87 };
88
89 /**
90 * Definition of a unique pointer holding a pool thread.
91 */
92 using UniquePoolThread = std::unique_ptr<PoolThread>;
93
94 /**
95 * Definition of a vector holding pool threads.
96 */
97 using PoolThreads = std::vector<UniquePoolThread>;
98
99 /**
100 * Definition of a queue holding functions.
101 */
102 using FunctionQueue = std::queue<Function>;
103
104 public:
105
106 /**
107 * Creates a new thread pool object.
108 */
109 ThreadPool() = default;
110
111 /**
112 * Destructs a thread pool.
113 */
114 ~ThreadPool() override;
115
116 /**
117 * Returns the maximal number of thread objects allowed inside this pool.
118 * @return Maximal thread capacity, with range [1, infinity); 2 by default
119 */
120 inline size_t capacity() const;
121
122 /**
123 * Returns the number of currently busy/active threads in this pool.
124 * @return Thread number, with range [0, infinity)
125 */
126 inline size_t size() const;
127
128 /**
129 * Defines the maximal number of thread objects existing concurrently.
130 * @param capacity Maximal number of thread objects to be allowed inside this pool, with range (1, infinity)
131 * @return True, if succeeded
132 */
133 bool setCapacity(const size_t capacity);
134
135 /**
136 * Invokes a function on one of the free threads of this pool.
137 * @param function The function that will be invoked by a free thread
138 * @return True, if the function will be invoked by a free thread, otherwise no free thread was available
139 */
140 bool invoke(Function&& function);
141
142 /**
143 * Returns the number of pending functions.
144 * @return The number of pending functions which have not been executed yet as the pool is at capacity, with range [0, infinity)
145 */
146 inline size_t pending() const;
147
148 /**
149 * Returns whether the ThreadPool has completed all jobs that have been submitted
150 * @return True, if the results of size() and pending() are atomically both 0
151 */
152 inline bool isEmpty() const;
153
154 protected:
155
156 /**
157 * Thread run function.
158 */
159 void threadRun() override;
160
161 protected:
162
163 /// The busy pool threads.
165
166 /// The idle pool threads.
168
169 /// The pending functions.
171
172 /// Maximal pool capacity, with range [1, infinity)
173 size_t capacity_ = 2;
174
175 /// The counter for pool thread ids.
176 size_t poolThreadIdCounter_ = 0;
177
178 /// Pool lock.
179 mutable Lock lock_;
180};
181
182/**
183 * This class wraps the ThreadPool into a singleton for global usage.
184 * Use the standard ThreadPool in case a singleton is not sufficient.
185 * @see ThreadPool.
186 * @ingroup base
187 */
189 public Singleton<ThreadPoolSingleton>,
190 public ThreadPool
191{
192 friend class Singleton<ThreadPoolSingleton>;
193
194 protected:
195
196 /**
197 * Protected default constructor.
198 */
200};
201
203{
204 const ScopedLock scopedLock(lock_);
205
206 return function_ != nullptr;
207}
208
209inline size_t ThreadPool::capacity() const
210{
211 const ScopedLock scopedLock(lock_);
212 return capacity_;
213}
214
215inline size_t ThreadPool::size() const
216{
217 const ScopedLock scopedLock(lock_);
218
219 return busyPoolThreads_.size();
220}
221
222inline size_t ThreadPool::pending() const
223{
224 const ScopedLock scopedLock(lock_);
225
226 return pendingFunctions_.size();
227}
228
229inline bool ThreadPool::isEmpty() const
230{
231 const ScopedLock scopedLock(lock_);
232
233 return (pendingFunctions_.size() + busyPoolThreads_.size()) == 0;
234}
235
236}
237
238#endif // META_OCEAN_BASE_THREAD_POOL_H
This class implements a recursive lock object.
Definition Lock.h:31
This class implements a scoped lock object for recursive lock objects.
Definition Lock.h:135
This class implements a signal.
Definition Signal.h:31
This template class is the base class for all singleton objects.
Definition Singleton.h:71
This class implements a thread.
Definition Thread.h:115
This class implements one thread of this thread pool.
Definition ThreadPool.h:44
void invoke(Function &&function)
Sets the thread function.
PoolThread(const std::string &name)
Creates a new thread object.
Lock lock_
Thread lock.
Definition ThreadPool.h:86
void threadRun() override
Thread run function.
bool isBusy() const
Returns whether this thread is currently busy.
Definition ThreadPool.h:202
Signal signal_
Internal signal handling the internal thread execution.
Definition ThreadPool.h:80
Function function_
The function that is invoked in this thread.
Definition ThreadPool.h:83
~PoolThread() override
Destructs a thread object.
This class implements a pool holding re-usable thread objects for individual use.
Definition ThreadPool.h:30
size_t capacity_
Maximal pool capacity, with range [1, infinity)
Definition ThreadPool.h:173
size_t size() const
Returns the number of currently busy/active threads in this pool.
Definition ThreadPool.h:215
FunctionQueue pendingFunctions_
The pending functions.
Definition ThreadPool.h:170
bool isEmpty() const
Returns whether the ThreadPool has completed all jobs that have been submitted.
Definition ThreadPool.h:229
size_t capacity() const
Returns the maximal number of thread objects allowed inside this pool.
Definition ThreadPool.h:209
ThreadPool()=default
Creates a new thread pool object.
PoolThreads idlePoolThreads_
The idle pool threads.
Definition ThreadPool.h:167
Lock lock_
Pool lock.
Definition ThreadPool.h:179
std::function< void()> Function
Definition of a caller function without return parameter.
Definition ThreadPool.h:36
std::queue< Function > FunctionQueue
Definition of a queue holding functions.
Definition ThreadPool.h:102
bool invoke(Function &&function)
Invokes a function on one of the free threads of this pool.
bool setCapacity(const size_t capacity)
Defines the maximal number of thread objects existing concurrently.
~ThreadPool() override
Destructs a thread pool.
void threadRun() override
Thread run function.
PoolThreads busyPoolThreads_
The busy pool threads.
Definition ThreadPool.h:164
size_t pending() const
Returns the number of pending functions.
Definition ThreadPool.h:222
std::vector< UniquePoolThread > PoolThreads
Definition of a vector holding pool threads.
Definition ThreadPool.h:97
std::unique_ptr< PoolThread > UniquePoolThread
Definition of a unique pointer holding a pool thread.
Definition ThreadPool.h:92
This class wraps the ThreadPool into a singleton for global usage.
Definition ThreadPool.h:191
ThreadPoolSingleton()
Protected default constructor.
The namespace covering the entire Ocean framework.
Definition Accessor.h:15