6#include "cdm/CommonDefs.h"
10#include <condition_variable>
15#include <condition_variable>
37 for (
size_t i = 0; i < numThreads; i++)
488 template<
class F,
class... Args>
489 auto enqueue(F&& f, Args&&... args)
490 ->std::future<
typename std::invoke_result<F,Args...>::type>;
497 std::queue< std::function<void()> >
tasks;
510 threads = std::thread::hardware_concurrency();
511 for (
size_t i = 0; i < threads; ++i)
517 std::function<void()> task;
520 std::unique_lock<std::mutex> lock(this->queue_mutex);
521 this->condition.wait(lock,
522 [this] { return this->stop || !this->tasks.empty(); });
523 if (this->stop && this->tasks.empty())
525 task = std::move(this->tasks.front());
536template<
class F,
class... Args>
538-> std::future<
typename std::invoke_result<F,Args...>::type>
540 using return_type =
typename std::invoke_result<F,Args...>::type;
542 auto task = std::make_shared< std::packaged_task<return_type()> >(
543 std::bind(std::forward<F>(f), std::forward<Args>(args)...)
546 std::future<return_type> res = task->get_future();
548 std::unique_lock<std::mutex> lock(queue_mutex);
552 throw std::runtime_error(
"enqueue on stopped ThreadPool");
554 tasks.emplace([task]() { (*task)(); });
556 condition.notify_one();
568 for (std::thread& worker :
workers)
Definition: ThreadPool.h:19
MultiThreadedVectorProcessor(const std::vector< T * > &v)
Definition: ThreadPool.h:21
std::vector< std::thread > m_Threads
Definition: ThreadPool.h:94
virtual ~MultiThreadedVectorProcessor()
Definition: ThreadPool.h:27
void ProcessVectorContents()
Definition: ThreadPool.h:50
std::mutex m_Mutex
Definition: ThreadPool.h:93
std::atomic< size_t > m_NextIdx
Definition: ThreadPool.h:87
bool m_Stop
Definition: ThreadPool.h:92
void Start(size_t numThreads)
Definition: ThreadPool.h:32
const std::vector< T * > & m_Vector
Definition: ThreadPool.h:89
void Stop()
Definition: ThreadPool.h:41
std::atomic< size_t > m_NumComplete
Definition: ThreadPool.h:88
void Run()
Definition: ThreadPool.h:58
Definition: ThreadPool.h:485
bool stop
Definition: ThreadPool.h:502
std::vector< std::thread > workers
Definition: ThreadPool.h:495
ThreadPool(size_t=0)
Definition: ThreadPool.h:506
auto enqueue(F &&f, Args &&... args) -> std::future< typename std::invoke_result< F, Args... >::type >
Definition: ThreadPool.h:537
~ThreadPool()
Definition: ThreadPool.h:561
std::condition_variable condition
Definition: ThreadPool.h:501
std::queue< std::function< void()> > tasks
Definition: ThreadPool.h:497
size_t workerCount() const
Definition: ThreadPool.h:491
std::mutex queue_mutex
Definition: ThreadPool.h:500