14 template <
typename ThreadSafeQueue = ConcurrentQueue<ThreadTask>,
typename LocalQueue = WorkStealingQueue<ThreadTask>>
20 inline static thread_local std::size_t
threadId = 0;
21 inline static thread_local LocalQueue*
localQueue =
nullptr;
28 for (std::size_t i = 0; i < threadCount; ++i) {
43 template <
typename Callable,
typename ...Args>
44 std::future<std::invoke_result_t<Callable, Args&&...>>
submit(Callable callable, Args&&... args) {
45 using ReturnType = std::invoke_result_t<Callable, Args&&...>;
46 std::packaged_task<ReturnType()> task(std::bind(std::move(callable), std::forward<Args>(args)...));
47 auto result = task.get_future();
80 std::size_t threadCount =
poolSize();
81 for (std::size_t i = 0; i < threadCount; ++i) {
82 std::size_t
id = (
threadId + 1 + i) % threadCount;
89 std::this_thread::yield();
93 void runTasks(std::stop_token token, std::size_t
id) {
97 while (!token.stop_requested()) {
ThreadSafeQueue poolQueue_
static thread_local std::size_t threadId
flow::Vector< LocalQueue > localQueues_
MultiQueueThreadPool(std::size_t threadCount=std::thread::hardware_concurrency() - 1)
flow::Vector< std::jthread > threads_
void runTasks(std::stop_token token, std::size_t id)
std::future< std::invoke_result_t< Callable, Args &&... > > submit(Callable callable, Args &&... args)
Submit a task to the thread-local queue. If the task is submit by an external thread outside of the t...
void runPendingTask()
Try run a task from the local queue. If the local queue is empty, it tries to run a task from the poo...
std::size_t poolSize() const
static thread_local LocalQueue * localQueue
A task that can be execute by a thread. Internally, it uses type erasure to store a std::packaged_tas...