CTPL - Modern and efficient C++ Thread Pool Library



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.


  • standard c++ language, tested to compile on MS Visual Studio 2013 (2012?), gcc 4.8.2 and mingw 4.8.1(with posix threads)
  • simple but effiecient solution, one header only, no need to compile a binary library
  • query the number of idle threads and resize the pool dynamically
  • one API to push to the thread pool any collable object: lambdas, functors, functions, result of bind expression
  • collable objects with variadic number of parameters plus index of the thread running the object
  • automatic template argument deduction
  • get returned value of any type with standard c++ futures
  • get fired exceptions with standard c++ futures
  • use for any purpose under Apache license
  • two variants, one depends on Boost Lockfree Queue library, http://boost.org, which is a header only library

Sample usage

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

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

void third(int id, const std::string & additional_param) {}

int main () {

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

p.push(first); // function

p.push(third, "additional_param");

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

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

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

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


  • Pushing a function to the pool

    Pushing a function to the pool

    Hello, Thank you so much for this library. I was wondering why I keep getting an error to push to the pool: all_errors.cpp:244:25: error: no matching function for call to ‘ctpl::thread_pool::push(int)’

    I am following your example to the tee and I am still getting this error. Any ideas?

    opened by prayritkhanna 2
  • std::unique_lock shadow bug

    std::unique_lock shadow bug

    https://github.com/vit-vit/CTPL/blob/437e135dbd94eb65b45533d9ce8ee28b5bd37b6d/ctpl_stl.h#L48 https://github.com/vit-vit/CTPL/blob/437e135dbd94eb65b45533d9ce8ee28b5bd37b6d/ctpl_stl.h#L54 https://github.com/vit-vit/CTPL/blob/437e135dbd94eb65b45533d9ce8ee28b5bd37b6d/ctpl.h#L137

    Hi, you have a shadow bug in the link above. It is caused by the default constructor of std::unique_lock. You are not locking your code.

    See this video on why it is a bug: https://youtu.be/lkgszkPnV8g?t=2068.

    Solution to the bug: Just name it.

    Best regards, Jack

    opened by chunheguo 1
  • Possible thread issue?

    Possible thread issue?

    It appears the mutex is being locked after pushing the data onto the queue, rather than before. Is this behavior correct?

        template<typename F, typename... Rest>
        auto push(F && f, Rest&&... rest) ->std::future<decltype(f(0, rest...))> {
            auto pck = std::make_shared<std::packaged_task<decltype(f(0, rest...))(int)>>(
                std::bind(std::forward<F>(f), std::placeholders::_1, std::forward<Rest>(rest)...)
            auto _f = new std::function<void(int id)>([pck](int id) {
            this->q.push(_f); // <-- see here...
            std::unique_lock<std::mutex> lock(this->mutex); // <-- ...and here
            return pck->get_future();
    opened by JimViebke 1
  • Function call that should not compile is able to be passed into push()

    Function call that should not compile is able to be passed into push()

    The following program demonstrates the issue. This is using the STL version.

    struct A {};
    void f(int, A&) {}
    int main()
    	ctpl::thread_pool pool(4);
    	A a;
    	// Does not compile
    	// So this shouldn't be expected to compile either, yet it does
    	//pool.push(f, std::move(a));
    opened by Ahajha 0
  • How to wait for all Tasks to finish?

    How to wait for all Tasks to finish?

    Sorry for writing this as an issue.

    I could not figure out how it is ment to wait for all Tasks to finish. If i use stop - the Threads get deleted and the threadpool ends.

    Isn't there something like p.WaitForEverythingDone()? Currently I use this in the main thread: while (p.n_idle() < THREAD_COUNT ) { } But thats not really a good idea to burn 1 cpu core just for waiting?

    opened by Ravenbs 2
  • Resize thread pool

    Resize thread pool


    In my code I create a thread pool with only one thread, and then push all the functions that I want to execute. After that, while the functions are being executed by that thread, I use another thread (that doesn't belong to the thread pool) to increase the number of threads in the thread pool, using the resize method. However, the resizing has no effect since the new threads I added do not execute anything, only the first thread (thread 0) executes the functions that are still in the queue.

    Does this happen because when I push the functions they are automatically associated with a thread? And in my case, since when I push the functions there is only thread 0 in the thread pool, all functions become associated with that thread?

    Also, is there a problem in calling resize from a function outside the thread pool?

    Thank you in advance for your help!

    opened by claudiacorreia60 0
  • Problem in passing my function to

    Problem in passing my function to "push" method

    Hello there,

    So, i have a function with this definition: static void Invoke( int id, std::unique_ptr<BaseService> svc );

    And tried to pass it to ctpl "push" method to be queued in thread-pool: pThreadPool->push( std::ref(App::Invoke), std::move( svc ) );

    But I get this error:

    /home/hadi/CLionProjects/App/App.cpp:211:27: error: no matching member function for call to 'push'
        pThreadPool->push( std::ref(App::Invoke), std::move( svc ) );
    /home/hadi/CLionProjects/App/include/cptl/ctpl.h:152:14: note: candidate template ignored: substitution failure [with F = std::__1::reference_wrapper<void (int, std::__1::unique_ptr<BaseService, std::__1::default_delete<BaseService> >)>, Rest = <std::__1::unique_ptr<BaseService, std::__1::default_delete<BaseService> >>]: call to implicitly-deleted copy constructor of 'std::__1::unique_ptr<BaseService, std::__1::default_delete<BaseService> >'
            auto push(F && f, Rest&&... rest) ->std::future<decltype(f(0, rest...))> {
                 ^                                                        ~~~~
    /home/hadi/CLionProjects/App/include/cptl/ctpl.h:171:14: note: candidate function template not viable: requires single argument 'f', but 2 arguments were provided
            auto push(F && f) ->std::future<decltype(f(0))> {
    1 error generated.
    opened by atari83 1
  • How to get threads details and termination?

    How to get threads details and termination?

    I have some questions, maybe someone could help. How to find out how many threads are working at the moment? How know if all threads are terminated? How to terminate a thread from inside of thread and from outside (from another thread)?

    Thanks in advance.

    opened by EndErr 0
Thread-pool - Thread pool implementation using c++11 threads

Table of Contents Introduction Build instructions Thread pool Queue Submit function Thread worker Usage example Use case#1 Use case#2 Use case#3 Futur

Mariano Trebino 655 Dec 27, 2022
Thread-pool-cpp - High performance C++11 thread pool

thread-pool-cpp It is highly scalable and fast. It is header only. No external dependencies, only standard library needed. It implements both work-ste

Andrey Kubarkov 542 Dec 17, 2022
A modern thread pool implementation based on C++20

thread-pool A simple, functional thread pool implementation using pure C++20. Features Built entirely with C++20 Enqueue tasks with or without trackin

Paul T 151 Dec 22, 2022
Pool is C++17 memory pool template with different implementations(algorithms)

Object Pool Description Pool is C++17 object(memory) pool template with different implementations(algorithms) The classic object pool pattern is a sof

KoynovStas 1 Nov 18, 2022
A easy to use multithreading thread pool library for C. It is a handy stream like job scheduler with an automatic garbage collector. This is a multithreaded job scheduler for non I/O bound computation.

A easy to use multithreading thread pool library for C. It is a handy stream-like job scheduler with an automatic garbage collector for non I/O bound computation.

Hyoung Min Suh 12 Jun 4, 2022
High Performance Linux C++ Network Programming Framework based on IO Multiplexing and Thread Pool

Kingpin is a C++ network programming framework based on TCP/IP + epoll + pthread, aims to implement a library for the high concurrent servers and clie

null 23 Oct 19, 2022
ThreadPool - A fastest, exception-safety and pure C++17 thread pool.

Warnings Since commit 468129863ec65c0b4ede02e8581bea682351a6d2, I move ThreadPool to C++17. (To use std::apply.) In addition, the rule of passing para

Han-Kuan Chen 124 Dec 28, 2022
A C++17 thread pool for high-performance scientific computing.

We present a modern C++17-compatible thread pool implementation, built from scratch with high-performance scientific computing in mind. The thread pool is implemented as a single lightweight and self-contained class, and does not have any dependencies other than the C++17 standard library, thus allowing a great degree of portability

Barak Shoshany 1.1k Jan 4, 2023
An easy to use C++ Thread Pool

mvThreadPool (This library is available under a free and permissive license) mvThreadPool is a simple to use header only C++ threadpool based on work

Jonathan Hoffstadt 30 Dec 8, 2022
An ultra-simple thread pool implementation for running void() functions in multiple worker threads

void_thread_pool.cpp © 2021 Dr Sebastien Sikora. [email protected] Updated 06/11/2021. What is it? void_thread_pool.cpp is an ultra-simple

Seb Sikora 1 Nov 19, 2021
EOSP ThreadPool is a header-only templated thread pool writtent in c++17.

EOSP Threadpool Description EOSP ThreadPool is a header-only templated thread pool writtent in c++17. It is designed to be easy to use while being abl

null 1 Apr 22, 2022
Work Stealing Thread Pool

wstpool Work Stealing Thread Pool, Header Only, C++ Threads Consistent with the C++ async/future programming model. Drop-in replacement for 'async' fo

Yasser Asmi 5 Oct 29, 2022
MAN - Man is Thread Pool in C++17

Introduction MAN is a ThreadPool wrote in C++17. The name is chosen because, at least in France, it is said that men are not able to do several things

Antoine MORRIER 6 Mar 6, 2022
ThreadPool - A simple C++11 Thread Pool implementation

ThreadPool A simple C++11 Thread Pool implementation. Basic usage: // create thread pool with 4 worker threads ThreadPool pool(4); // enqueue and sto

Jakob Progsch 6.1k Jan 7, 2023
BabyCoin: mining pool

BabyCoin Pool Based on cryptonote-nodejs-pool cryptonote-nodejs-pool High performance Node.js (with native C addons) mining pool for CryptoNote based

null 1 May 15, 2022
Objectpool - Object pool implementation in C++11

Object pool allocator This is a C++11 implementation of an object pool allocator. For more information on object pool allocators and their purpose see

Cameron Hart 68 Nov 3, 2022
Simple and fast C library implementing a thread-safe API to manage hash-tables, linked lists, lock-free ring buffers and queues

libhl C library implementing a set of APIs to efficiently manage some basic data structures such as : hashtables, linked lists, queues, trees, ringbuf

Andrea Guzzo 392 Dec 3, 2022
Small library helping you with basic stuff like getting metrics out of your code, thread naming, etc.

CommonPP commonpp is a multi purpose library easing very few operations like: Getting metrics out of your program (counter, gauge, statistical descrip

Thomas Sanchez 28 Oct 31, 2022
OOX: Out-of-Order Executor library. Yet another approach to efficient and scalable tasking API and task scheduling.

OOX Out-of-Order Executor library. Yet another approach to efficient and scalable tasking API and task scheduling. Try it Requirements: Install cmake,

Intel Corporation 18 Oct 25, 2022