1#ifndef LIPH_CONCURRENCY_THREADPOOL_H_
2#define LIPH_CONCURRENCY_THREADPOOL_H_
5#include <condition_variable>
29 template <
class Function,
class... Args>
30 void add(Function&&
func, Args&&...args) {
31 if (stopped_)
throw std::runtime_error(
"Thread pool already stopped or not started");
32 std::unique_lock<std::mutex> lock(lock_);
33 tasks_.
push(std::bind(
func, std::forward<Args>(args)...));
37 unsigned int size()
const {
return size_; }
40 using task = std::function<void()>;
46 std::atomic<bool> stopped_;
47 std::atomic<bool> shutdown_;
48 std::vector<std::thread> threads_;
50 std::condition_variable cv_;
void push(const T &v)
Definition: blocking_queue.h:25
Definition: threadpool.h:16
threadpool & operator=(const threadpool &)=delete
threadpool(threadpool &&)=delete
void add(Function &&func, Args &&...args)
Definition: threadpool.h:30
threadpool & operator=(threadpool &&)=delete
threadpool(size_t size=0)
Definition: threadpool.cpp:5
unsigned int size() const
Definition: threadpool.h:37
~threadpool()
Definition: threadpool.cpp:12
threadpool(const threadpool &)=delete
void start()
Definition: threadpool.cpp:14
void stop()
Definition: threadpool.cpp:20
Definition: algorithm.h:10