21 #include <condition_variable> 59 LUNCHBOX_API
size_t getSize()
const;
66 inline std::future<typename std::result_of<F()>::type>
post(F&& f);
81 ThreadPool& operator=(
const ThreadPool&) =
delete;
82 ThreadPool& operator=(ThreadPool&&) =
delete;
84 LUNCHBOX_API
void joinAll();
85 LUNCHBOX_API
void work();
87 std::vector<std::thread> _threads;
88 std::queue<std::function<void()> > _tasks;
89 mutable std::mutex _mutex;
90 std::condition_variable _condition;
97 using ReturnType =
typename std::result_of<F()>::type;
100 std::make_shared<std::packaged_task<ReturnType()> >(std::forward<F>(f));
102 auto res = task->get_future();
104 std::unique_lock<std::mutex> lock(_mutex);
105 _tasks.emplace([task]() { (*task)(); });
107 _condition.notify_one();
111 template <
typename F>
115 std::unique_lock<std::mutex> lock(_mutex);
118 _condition.notify_one();
Thread pool for tasks execution.
Defines export visibility macros for library Lunchbox.
bool hasPendingJobs() const
~ThreadPool()
Destroy this thread pool.
std::future< typename std::result_of< F()>::type > post(F &&f)
Post a new task in the thread pool.
static ThreadPool & getInstance()
void postDetached(F &&f)
Post a detached task in the thread pool.
Abstraction layer and common utilities for multi-threaded programming.
ThreadPool(const size_t size)
Construct a new ThreadPool.