An example on how to use the ThreadPool class to schedule tasks.
#include <iostream>
using std::cout;
using namespace std::literals;
int main()
{
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())
cout << "Task result: " << task.get_result() << "\n";
pool->add_task(
[](ThreadPool& pool) {
cout << "Task 4\n";
pool.add_task([]() { cout << "Task 5, a second later\n"; }, 1s);
});
return 0;
}
Declaration of the ThreadPool class.
std::shared_ptr< ThreadPool > make_thread_pool(std::size_t num_threads, std::size_t capacity=ThreadPool::default_capacity)
Create a thread pool with the desired number of threads and the specified capacity for queuing tasks.
Definition: ThreadPool.h:599
bool sleep(const std::chrono::duration< Rep, Period > &duration, const Trigger &trg)
Sleep for at least the given time span, with the option of being woken up from another thread.
Definition: time_util.h:114
Namespace gul14 contains all functions and classes of the General Utility Library.
Definition: doxygen.h:26
Declaration of time related functions for the General Utility Library.