Thread pool - Thread pool using std::* primitives from C++17, with optional priority queue/greenthreading for POSIX.



Thread pool using std::* primitives from C++11. Also includes a class for a priority thread pool.

Requires concepts and C++17, including constexpr-if. Currently only GCC 7.0+ is sufficient. Use -std=c++17 -fconcepts to compile. The priority thread pool is only supported on POSIX/-like systems. But it's still easy to use the normal pool on non-POSIX; just don't compile priority_thread_pool.cpp or include the header.

If GCC 6.2 is prefered, revision 65879c9 is compatible. For just C++11, use 8bdfb9b. 5ea01d0 was the latest to support <= C++14.

The priority pool has the same API as described below, accept it has an int parameter first for the priority of the task. E.g. pool.async(5, func, arg1, arg2) for priority 5.

An example that computes the primality of 2 to 10,000 using 8 threads:

#include "thread_pool.hpp"

#include <iostream>
#include <list>
#include <utility>

using namespace std;

// Return the integer argument and a boolean representing its primality.
// We need to return the integer because the loop that retrieves results doesn't know which integer corresponds to which future.
pair<int, bool> is_prime(int n){
  for(int i = 2;i < n;i++)
    if(n % i == 0)
      return make_pair(i, false);
  return make_pair(n, true);

int main(){
  thread_pool pool(8);                   // Contruct a thread pool with 8 threads.
  list<future<pair<int, bool>>> results;
  for(int i = 2;i < 10000;i++){
  	// Add a task to the queue.
    results.push_back(pool.async(is_prime, i));
  for(auto i = results.begin();i != results.end();i++){
    pair<int, bool> result = i->get();  // Get the pair<int, bool> from the future<...>
    cout << result.first << ": " << (result.second ? "is prime" : "is composite") << endl;
  return 0;

thread_pool::async is a templated method that accepts any std::function and arguments to pass to the function. It returns a std::future<Ret> where Ret is the return type of the aforementioned std::function.

To submit a task: future<Ret> fut(pool.async(func, args...));.

To wait on fut to complete: Ret result = fut.get();

  • Test as number of threads decreased : infinite loop?

    Test as number of threads decreased : infinite loop?

    I simplified the original test code to be the following


    `#include "thread_pool.hpp" #include <vector> #include <iostream> // std::cout, std::endl using namespace std;


    • Intel Xeon ES-1620 v3 3.5GHz
    • git clone
    • git checkout -b cv origin/cv
    • $ gcc --version
    • gcc.exe (Rev3, Built by MSYS2 project) 6.3.0
    • $ g++ --std=c++11 test.cpp thread_pool.cpp
    • $ time ./a.exe
    • ...
    • real 5m23.883s for num_threads = 1 (aborted manually)
    • real 3m55.198s for num_threads = 2 (aborted manually)
    • why do above configurations never finish but the ones below do finish?
    • Further, if add "-O3" to compiler option above, the run in 26s and 13s,why?
    • real 0m12.690s for num_threads = 3
    • real 0m9.199s for num_threads = 4
    • real 0m7.971s for num_threads = 5
    • real 0m7.654s for num_threads = 6
    • real 0m7.304s for num_threads = 7
    • real 0m7.740s for num_threads = 8 */

    bool func_bool_int(int n) { if(n < 2) return false; for(int i = 2;i < n;i++) if(n % i == 0) return false; return true; }

    int main() {

    const int num_threads = 4; thread_pool p(num_threads);

    int n = 100000;
    int max = 5 * n;
    vector<future<bool>> f;
    for( int i = n; i < max; i++ )
    	f.emplace_back(p.async(function<bool(int)>(func_bool_int), i));

    std::cout << "..." << std::endl; size_t accum = 0; for(auto &i : f) { accum += i.get(); } std::cout << "accum = " << accum << std::endl;

    return 0;

    } `

    When I ran for num_threads = 8, 7, 6, 5, 4, 3 things went okay. But when I tried num_threads of 2 or 1 the runtime seemed to be in an infinite loop ie I was seeing about 7s to 12s runtimes above num_threads>2 but once I tried num_threads=2 or 1 I waited 3 minutes to 5 minutes (orders of magnitude longer than extrapolated runtime) before manually killing the process. Is this behavior to be expected? Note this was on a gcc 6.3 with "git checkout -b cv origin/cv" branch. Adding an "-O3" optimazation seemed to fix the issue; expected?

    opened by pocdn 4
  • High CPU usage

    High CPU usage

    I integrated thread_pool in one of our projects and replaced the current threading mechanism. Instead of creating new threads I wanted to reuse the threads. It was fairly easy to migrate to this lib, but I am experiencing high CPU load, when the thread pool is not used.

    opened by r0l1 2
  • How to pass a multiple arguments to my function?

    How to pass a multiple arguments to my function?

    I tried something like

    int myFunc(int a, int b, int c)
    ... myPool.async(std::function<int(int, int, int)>(myFunc), a, b, c)...

    and get a ton of errors. But if I using function with 1 argument everything works well. What i'm doing wrong?

    opened by lkjhgfda 2
  • Waiting for threads to finish

    Waiting for threads to finish


    How do I wait for threads to finish?

    I've got this:

        fut.emplace_back(tp.async(std::function<int(std::string)>(breakPacket), completePacket));

    I push into it about 3000 items for the threads ti process, but then I don't know how to wait for the threads to finish...



    opened by davidcsi 2
  • Error: underined reference to thread_pool::...

    Error: underined reference to thread_pool::...

    (Edited by Tyler to show error that OP had before he deleted the comment): ... main.cpp:(.text+0x31b): undefined reference to 'thread_pool::~thread_pool()' ...

    opened by davidcsi 2
  • Compilation warning Wsign-compare

    Compilation warning Wsign-compare

    Very silly, but annoying; I get a:

    thread_pool.hpp:107:19: warning: comparison of integer expressions of different signedness: ‘int’ and ‘unsigned int’ [-Wsign-compare]

    warning, which is easily silenced by:

    - for(int i = 0;i < num_threads;i++){
    + for(unsigned int i = 0;i < num_threads;i++){
    opened by dpellegr 0
Tyler Hardin
Tyler Hardin
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 595 Jun 26, 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 531 May 27, 2022
Sqrt OS is a simulation of an OS scheduler and memory manager using different scheduling algorithms including Highest Priority First (non-preemptive), Shortest Remaining Time Next, and Round Robin

A CPU scheduler determines an order for the execution of its scheduled processes; it decides which process will run according to a certain data structure that keeps track of the processes in the system and their status.

null 11 Jul 14, 2021
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 Feb 14, 2022
Concurrency Kit 2.1k Jun 18, 2022
Optimized primitives for collective multi-GPU communication

NCCL Optimized primitives for inter-GPU communication. Introduction NCCL (pronounced "Nickel") is a stand-alone library of standard communication rout

NVIDIA Corporation 1.8k Jun 17, 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
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 774 Jun 30, 2022
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 Jun 7, 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
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 14 Jun 19, 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 3 Jul 20, 2021
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 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 121 Jun 4, 2022
CTPL - Modern and efficient C++ Thread Pool Library

CTPL Modern and efficient C++ Thread Pool Library A thread pool is a programming pattern for parallel execution of jobs,

null 1k Jun 20, 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 5.6k Jun 24, 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 98 Jun 7, 2022
A fast multi-producer, multi-consumer lock-free concurrent queue for C++11

moodycamel::ConcurrentQueue An industrial-strength lock-free queue for C++. Note: If all you need is a single-producer, single-consumer queue, I have

Cameron 6.8k Jun 24, 2022