7 #include "fwThread/Pool.hpp" 10 #include <fwCore/util/LazyInstantiator.hpp> 18 Pool(
std::thread::hardware_concurrency())
27 const auto availableCores = std::thread::hardware_concurrency();
28 OSLM_WARN_IF( _threads <<
" threads were allocated in this thread pool, but you only have " <<
29 availableCores <<
" physical cores on this CPU",
30 _threads > std::thread::hardware_concurrency());
32 for(
size_t i = 0; i < _threads; ++i)
34 m_workers.emplace_back(
39 std::function<void()> task;
42 std::unique_lock<std::mutex> lock(m_queueMutex);
43 m_condition.wait(lock, [
this] {
return m_stop || !m_tasks.empty(); });
44 if(m_stop && m_tasks.empty())
48 task = std::move(m_tasks.front());
64 std::unique_lock<std::mutex> lock(m_queueMutex);
68 m_condition.notify_all();
69 for(std::thread& worker: m_workers)
FWTHREAD_API Pool & getDefaultPool()
Get the default pool.
static InstanceSptrType getInstance()
Returns the singleton instance. This method is thread safe.
FWTHREAD_API Pool()
this constructor launches as much as possible workers
This class creates and manages a pool of threads which process tasks.
FWTHREAD_API ~Pool()
the destructor joins all threads
This namespace fwThread provides few tools to execute asynchronous tasks on different threads...
#define OSLM_WARN_IF(message, cond)
This file defines SpyLog macros. These macros are used to log messages to a file or to the console du...