14 template <
typename ThreadSafeQueue = ConcurrentQueue<ThreadTask>>
20 explicit SimpleThreadPool(std::size_t threadCount = std::thread::hardware_concurrency() - 1) {
22 for (std::size_t i = 0; i < threadCount; ++i) {
35 template <
typename Callable,
typename ...Args>
36 std::future<std::invoke_result_t<Callable, Args&&...>>
submit(Callable callable, Args&&... args) {
37 using ReturnType = std::invoke_result_t<Callable, Args&&...>;
38 std::packaged_task<ReturnType()> task(std::bind(std::move(callable), std::forward<Args>(args)...));
39 auto result = task.get_future();
48 if (
auto task =
queue_.tryPop(); task) {
51 std::this_thread::yield();
57 while (!token.stop_requested()) {
std::future< std::invoke_result_t< Callable, Args &&... > > submit(Callable callable, Args &&... args)
Submit a task to the thread pool queue.
void runTasks(std::stop_token token)
SimpleThreadPool(std::size_t threadCount=std::thread::hardware_concurrency() - 1)
flow::Vector< std::jthread > threads_
void runPendingTask()
Try run a task from the pool queue. If the queue is empty, then it yields the thread....
std::size_t poolSize() const
A task that can be execute by a thread. Internally, it uses type erasure to store a std::packaged_tas...