An example on how to use the ThreadPool class to schedule tasks.
An example on how to use the ThreadPool class to schedule tasks.
#include <iostream>
using std::cout;
using namespace std::literals;
int main()
{
auto pool = make_thread_pool(2);
pool->add_task([]() { cout << "Task 1\n"; });
pool->add_task([]() { sleep(1); std::cout << "Task 2\n"; });
pool->add_task([]() { cout << "Task 3\n"; }, 2s);
auto task = pool->add_task([]() { return 42; });
while (not task.is_complete())
sleep(0.1);
cout << "Task result: " << task.get_result() << "\n";
pool->add_task(
cout << "Task 4\n";
pool.
add_task([]() { cout <<
"Task 5, a second later\n"; }, 1s);
});
return 0;
}
Declaration of the ThreadPool class.
A pool of worker threads with a task queue.
Definition ThreadPool.h:111
TaskHandle< std::invoke_result_t< Function, ThreadPool & > > add_task(Function fct, TimePoint start_time={}, std::string name={})
Enqueue a task.
Definition ThreadPool.h:309
Namespace gul17 contains all functions and classes of the General Utility Library.
Definition doxygen.h:26
Declaration of time related functions for the General Utility Library.