OpenLB 1.7
Loading...
Searching...
No Matches
Public Member Functions | List of all members
olb::ThreadPool Class Reference

Pool of threads for CPU-based background processing. More...

#include <threadPool.h>

+ Collaboration diagram for olb::ThreadPool:

Public Member Functions

 ThreadPool ()
 
void init (int nThreads, bool verbose)
 Initialization to be called by olbInit.
 
 ~ThreadPool ()
 
unsigned size () const
 Returns number of threads.
 
template<typename F >
void scheduleAndForget (F &&f)
 Schedule F, tracking neither its return value nor completion.
 
template<typename F , typename R = std::invoke_result_t<std::decay_t<F>>>
std::future< R > schedule (F &&f)
 Schedule F and return future of its return value.
 
void wait ()
 Blocks until all tasks are completed.
 
template<typename T >
void waitFor (std::vector< std::future< T > > &futures)
 Blocks until all tasks producing the given futures are completed.
 

Detailed Description

Pool of threads for CPU-based background processing.

Not intended for parallelization of performance critical paths but for background processing of e.g. VTK output to reduce blocking of simulation process.

Definition at line 47 of file threadPool.h.

Constructor & Destructor Documentation

◆ ThreadPool()

olb::ThreadPool::ThreadPool ( )
inline

Definition at line 86 of file threadPool.h.

86 :
87 _mutex{},
88 _waiting{false},
89 _active{true},
90 _taskCount{0},
91 _available{},
92 _done{},
93 _threads{1},
94 _queue{},
95 _initialized{false}
96 { }

◆ ~ThreadPool()

olb::ThreadPool::~ThreadPool ( )
inline

Definition at line 115 of file threadPool.h.

116 {
117 _active = false;
118 _available.notify_all();
119 for (std::thread& thread : _threads) {
120 thread.join();
121 }
122 }

Member Function Documentation

◆ init()

void olb::ThreadPool::init ( int nThreads,
bool verbose )
inline

Initialization to be called by olbInit.

Definition at line 99 of file threadPool.h.

100 {
101 OLB_PRECONDITION(!_initialized);
102 OstreamManager clout(std::cout, "ThreadPool");
103 if (nThreads > 1) {
104 _threads.resize(nThreads);
105 }
106 for (unsigned iThread=0; iThread < _threads.size(); ++iThread) {
107 _threads[iThread] = std::thread(&ThreadPool::work, this, iThread);
108 }
109 if (verbose) {
110 clout << "Sucessfully initialized, numThreads=" << std::to_string(_threads.size()) << std::endl;
111 }
112 _initialized = true;
113 }
#define OLB_PRECONDITION(COND)
Definition olbDebug.h:46

References OLB_PRECONDITION.

+ Here is the caller graph for this function:

◆ schedule()

template<typename F , typename R = std::invoke_result_t<std::decay_t<F>>>
std::future< R > olb::ThreadPool::schedule ( F && f)
inline

Schedule F and return future of its return value.

Definition at line 145 of file threadPool.h.

146 {
147 OLB_PRECONDITION(_initialized);
148 auto packagedF = std::make_shared<std::packaged_task<R()>>(f);
149 {
150 const std::scoped_lock lock(_mutex);
151 _queue.push([packagedF]() {
152 (*packagedF)();
153 });
154 }
155 ++_taskCount;
156 _available.notify_one();
157 return packagedF->get_future();
158 }

References OLB_PRECONDITION.

+ Here is the caller graph for this function:

◆ scheduleAndForget()

template<typename F >
void olb::ThreadPool::scheduleAndForget ( F && f)
inline

Schedule F, tracking neither its return value nor completion.

Definition at line 132 of file threadPool.h.

133 {
134 OLB_PRECONDITION(_initialized);
135 {
136 const std::scoped_lock lock(_mutex);
137 _queue.push(f);
138 }
139 ++_taskCount;
140 _available.notify_one();
141 }

References OLB_PRECONDITION.

◆ size()

unsigned olb::ThreadPool::size ( ) const
inline

Returns number of threads.

Definition at line 125 of file threadPool.h.

126 {
127 return _threads.size();
128 }

◆ wait()

void olb::ThreadPool::wait ( )
inline

Blocks until all tasks are completed.

Definition at line 161 of file threadPool.h.

162 {
163 OLB_PRECONDITION(_initialized);
164 _waiting = true;
165 std::unique_lock lock(_mutex);
166 _done.wait(lock, [&]() {
167 return _taskCount == 0;
168 });
169 _waiting = false;
170 }

References OLB_PRECONDITION.

◆ waitFor()

template<typename T >
void olb::ThreadPool::waitFor ( std::vector< std::future< T > > & futures)
inline

Blocks until all tasks producing the given futures are completed.

Definition at line 174 of file threadPool.h.

175 {
176 for (auto& future : futures) {
177 future.wait();
178 }
179 }
+ Here is the caller graph for this function:

The documentation for this class was generated from the following file: