Pure C++14 Thread Pool Executor. No external dependencies.
- C++14
#include <iostream>
#include <sstream>
#include <iomanip>
#include <functional>
#include <thread>
#include <atomic>
#include <random>
#include "thread_pool_executor.hpp"
using namespace std::chrono_literals;
std::random_device rand_dev;
thread_local size_t worker_idx = -1;
std::atomic_size_t workers_cnt;
void task(size_t i)
{
if (worker_idx == -1) {
worker_idx = ++workers_cnt;
}
std::stringstream message;
std::uniform_int_distribution<int> dist(0, 10);
std::this_thread::sleep_for(std::chrono::seconds(dist(rand_dev)));
message << "[" << "worker-" << std::setfill('0') << std::setw(2) << worker_idx << "]" << "\t"
<< "task-" << std::setw(2) << i << " has been compleated." << std::endl;
std::cout << message.str();
}
int main()
{
std::uniform_int_distribution<int> dist(0, 500);
size_t pool_size = 4;
size_t max_pool_size = 16;
size_t max_queue_size = 64;
std::chrono::seconds keep_alive_time = 5s;
ThreadPoolExecutor executor(pool_size, max_pool_size, keep_alive_time, max_queue_size);
for (size_t i = 0; i < 100; ++i) {
executor.submit(std::bind(task, i));
std::this_thread::sleep_for(std::chrono::milliseconds(dist(rand_dev)));
}
executor.shutdown();
executor.wait();
return 0;
}
Creates a thread pool executor instance and starts pool_size
threads.
Constructor arguments:
pool_size
- the number of threads to keep in the pool, even if they are idlemax_pool_size
- the maximum number of threads to allow in the poolkeep_alive_time
- when the number of threads is greater than thepool_size
, this is the maximum time that excess idle threads will wait for new tasks before terminatingmax_queue_size
- the maximum number of tasks in the executor queue, if themax_queue_size
is reachedQueueIsFull
exception will be thrown
Executes the given task sometime in the future.
Method arguments:
func
- the task to execute
Returns true if this executor has been shut down.
Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted.
Initiates a termination process in which previously submitted tasks waiting in the queue will be destroyed, no new tasks will be accepted.
Returns true if this executor has been is_terminated.
Blocks until all tasks have completed execution after a shutdown request, or the timeout occurs, whichever happens first.
Method arguments:
timeout
- the maximum time to wait
Public Domain License