17 unsigned int queueDepth = std::thread::hardware_concurrency(),
18 size_t threads = std::thread::hardware_concurrency())
19 : m_workQueue(queueDepth)
21 assert(queueDepth != 0);
23 for (
size_t i = 0; i < threads; ++i)
24 m_threads.emplace_back(std::thread([
this]() {
27 auto workItem = m_workQueue.pop();
28 if (workItem == nullptr)
30 m_workQueue.push(nullptr);
40 m_workQueue.push(
nullptr);
41 for (
auto& thread : m_threads)
45 using Proc = std::function<void(
void)>;
47 template<
typename F,
typename... Args>
50 m_workQueue.push([=]() { std::invoke(f, args...); });
53 template<
typename F,
typename... Args>
54 auto enqueue_task(F&& f, Args&&... args) -> std::future<
typename std::result_of<F(Args...)>::type>
56 using return_type =
typename std::result_of<F(Args...)>::type;
57 auto task = std::make_shared<std::packaged_task<return_type()>>(std::bind(std::forward<F>(f), std::forward<Args>(args)...));
58 std::future<return_type> res = task->get_future();
59 m_workQueue.push([task]() { (*task)(); });
64 using ThreadPool = std::vector<std::thread>;
std::function< void(void)> Proc
auto enqueue_task(F &&f, Args &&... args) -> std::future< typename std::result_of< F(Args...)>::type >
void enqueue_work(F &&f, Args &&... args)
thread_pool(unsigned int queueDepth=std::thread::hardware_concurrency(), size_t threads=std::thread::hardware_concurrency())