Skip to content

Commit c6d0ce6

Browse files
committed
minor: fix test
1 parent 1da52cb commit c6d0ce6

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

cpp/support/thread_pool.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <condition_variable>
1111
#include <cstddef>
1212
#include <functional>
13+
#include <future>
1314
#include <mutex>
1415
#include <queue>
1516
#include <thread>
@@ -59,6 +60,37 @@ class ThreadPool {
5960
}
6061
}
6162

63+
/*!
64+
* \brief Add a new task to be executed by the thread pool.
65+
* \tparam F Type of the function to execute
66+
* \tparam Args Types of the arguments to pass to the function
67+
* \param f Function to execute
68+
* \param args Arguments to pass to the function
69+
* \return std::shared_future containing the result of the function call
70+
* \note Tasks are executed in FIFO order but may complete in any order.
71+
*/
72+
template <class F, class... Args>
73+
auto Submit(F&& f, Args&&... args) -> std::shared_future<std::invoke_result_t<F, Args...>> {
74+
using return_type = std::invoke_result_t<F, Args...>;
75+
76+
// Package the task with its arguments into a shared pointer
77+
auto task = std::make_shared<std::packaged_task<return_type()>>(
78+
std::bind(std::forward<F>(f), std::forward<Args>(args)...)
79+
);
80+
auto res = task->get_future().share();
81+
82+
{
83+
std::unique_lock<std::mutex> lock(queue_mutex_);
84+
XGRAMMAR_CHECK(!shutdown_) << "Cannot submit task to stopped ThreadPool";
85+
++unfinished_task_count_; // Increment task count
86+
87+
// Directly add the task without wrapping
88+
task_queue_.emplace([task]() { (*task)(); });
89+
}
90+
queue_condition_.notify_one();
91+
return res;
92+
}
93+
6294
/*!
6395
* \brief Add a new task to be executed by the thread pool without returning a future.
6496
* \tparam F Type of the function to execute

0 commit comments

Comments
 (0)