Skip to content

Commit

Permalink
Add: an auto queue function
Browse files Browse the repository at this point in the history
  • Loading branch information
mjshakir committed Nov 30, 2024
1 parent 6708cf5 commit 400e2f0
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions include/ThreadPool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,38 @@ namespace ThreadPool {
//--------------------------
}// end TaskBuilder queue(F&& f, Args&&... args)// end TaskBuilder queue(F&& f, Args&&... args)
//--------------------------
/**
* @brief Enqueues a task with priority and retry settings by returning a TaskBuilder object.
*
* @details This method creates a TaskBuilder instance that allows the caller to set additional
* properties for the task such as priority and number of retries. This overload of `enqueue` is
* specifically enabled when `use_priority_queue` is true, allowing tasks to be queued in a priority-based fashion.
*
* @note The task is automatically submitted to the thread pool without the need TaskBuilder's `submit` method to be called.
*
* @tparam F Type of the callable to be executed
* @tparam Args Variadic template for the arguments list of the callable
* @return TaskBuilder<F, Args...> A builder object for setting task properties and submitting the task.
* @example
* @code
* ThreadPool pool; // ThreadPool must be instantiated with priority queue support enabled.
* auto task = pool.queue([](int a, int b) { return a + b; }, 2, 3);
* task.set_priority(5).set_retries(2); // Optional: set priority and retries.
* auto result = task.get(); // Will wait for the task to complete and retrieve the result.
* @endcode
* @code
* ThreadPool pool; // ThreadPool must be instantiated with priority queue support enabled.
* auto task = pool.queue([](int a, int b) { return a + b; }, 2, 3).set_priority(5).set_retries(2); // Optional: set priority and retries.
* auto result = task.get(); // Will wait for the task to complete and retrieve the result.
* @endcode
*/
template <class F, class... Args>
std::enable_if_t<static_cast<bool>(use_priority_queue), TaskBuilder<F, Args...>> queue(F&& f, Args&&... args) {
//--------------------------
return TaskBuilder<F, Args...>(*this, true, std::forward<F>(f), std::forward<Args>(args)...);
//--------------------------
}// end TaskBuilder queue(F&& f, Args&&... args)// end TaskBuilder queue(F&& f, Args&&... args)
//--------------------------
/**
* @brief Queue a new task in the thread pool.
*
Expand Down

0 comments on commit 400e2f0

Please sign in to comment.