Home

Awesome

CTPL

Modern and efficient C++ Thread Pool Library

A thread pool is a programming pattern for parallel execution of jobs, http://en.wikipedia.org/wiki/Thread_pool_pattern.

More specifically, there are some threads dedicated to the pool and a container of jobs. The jobs come to the pool dynamically. A job is fetched and deleted from the container when there is an idle thread. The job is then run on that thread.

A thread pool is helpful when you want to minimize time of loading and destroying threads and when you want to limit the number of parallel jobs that run simultanuasly. For example, time consuming event handlers may be processed in a thread pool to make UI more responsive.

Features:

Sample usage

<code>void first(int id) { std::cout << "hello from " << id << '\n'; }</code>

<code> struct Second { void operator()(int id) const { std::cout << "hello from " << id << '\n'; } } second;

<code>void third(int id, const std::string & additional_param) {}</code>

<code>int main () {</code>

<code> ctpl::thread_pool p(2 /* two threads in the pool */);</code>

<code> p.push(first); // function</code>

<code> p.push(third, "additional_param");</code>

<code> p.push( [] (int id){ std::cout << "hello from " << id << '\n'; }); // lambda</code>

<code> p.push(std::ref(second)); // functor, reference</code>

<code> p.push(const_cast<const Second &>(second)); // functor, copy ctor</code>

<code> p.push(std::move(second)); // functor, move ctor</code>

<code>}</code>