|
10 | 10 | #include <condition_variable> |
11 | 11 | #include <cstddef> |
12 | 12 | #include <functional> |
| 13 | +#include <future> |
13 | 14 | #include <mutex> |
14 | 15 | #include <queue> |
15 | 16 | #include <thread> |
@@ -59,6 +60,37 @@ class ThreadPool { |
59 | 60 | } |
60 | 61 | } |
61 | 62 |
|
| 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 | + |
62 | 94 | /*! |
63 | 95 | * \brief Add a new task to be executed by the thread pool without returning a future. |
64 | 96 | * \tparam F Type of the function to execute |
|
0 commit comments