14namespace thread_pool_util {
26 std::vector<std::thread> _thread;
28 std::condition_variable _cv;
46 for (
auto &t : _thread) {
67 template<
class Function,
class... Args>
68 auto push(Function &&newTask, Args &&...
args) {
69 std::lock_guard lg(_lock);
70 auto future = TaskPool::push(std::forward<Function>(newTask), std::forward<Args>(
args)...);
82 std::lock_guard lg(_lock);
84 TaskPool::pushDelayed(std::move(task));
95 template<
class Function,
class X,
class Y,
class... Args>
96 auto pushDelayed(Function &&newTask, std::chrono::duration<X, Y> duration, Args &&...
args) {
97 std::lock_guard lg(_lock);
98 auto future = TaskPool::pushDelayed(std::forward<Function>(newTask), duration, std::forward<Args>(
args)...);
113 _thread.resize(threads);
115 for (
auto &t : _thread) {
124 std::lock_guard lg(_lock);
134 for (
auto &t : _thread) {
144 platf::set_thread_name(
"TaskPool::worker");
146 if (
auto task = this->
pop()) {
149 std::unique_lock uniq_lock(_lock);
159 if (
auto tp =
next()) {
160 _cv.wait_until(uniq_lock, *tp);
168 while (
auto task = this->
pop()) {
Queue of immediate and delayed tasks executed by worker threads.
Definition task_pool.h:67
std::optional< __task > pop()
Remove and return the next queued item, waiting when requested.
Definition task_pool.h:301
std::optional< __time_point > next()
Return the next timer task ready for execution.
Definition task_pool.h:335
bool ready()
Check whether the task pool has active workers.
Definition task_pool.h:324
Definition thread_pool.h:18
auto pushDelayed(Function &&newTask, std::chrono::duration< X, Y > duration, Args &&...args)
Queue a task that becomes runnable after a delay.
Definition thread_pool.h:96
ThreadPool(int threads)
Start a pool with the requested number of worker threads.
Definition thread_pool.h:43
void pushDelayed(std::pair< __time_point, __task > &&task)
Queue a task that becomes runnable after a delay.
Definition thread_pool.h:81
void start(int threads)
Start worker threads for queued thread-pool tasks.
Definition thread_pool.h:110
void join()
Wait for worker threads owned by the session to exit.
Definition thread_pool.h:133
void stop()
Stop worker threads and prevent additional task execution.
Definition thread_pool.h:123
TaskPool::__task __task
Callable unit executed by a worker thread.
Definition thread_pool.h:23
void _main()
Run the main application or worker loop.
Definition thread_pool.h:143
auto push(Function &&newTask, Args &&...args)
Queue work for asynchronous execution.
Definition thread_pool.h:68
Declarations for common platform specific utilities.
Functions for handling command line arguments.
Definition entry_handler.cpp:37
Declarations for the task pool system.