ThreadPool - A fastest, exception-safety and pure C++17 thread pool.

Overview

Warnings

Since commit 468129863ec65c0b4ede02e8581bea682351a6d2, I move ThreadPool to C++17. (To use std::apply.)
In addition, the rule of passing parameters to ThreadPool is different.
Unlike before, which uses std::bind, ThreadPool will not copy anything right now.
All of ThreadPool does is forward (no decay).
This means you have to copy arguments by yourself before passing it to ThreadPool.
Below is demonstration,

void test(int &i)
{
	i=10;
}

int main()
{
	int i(0);
	CThreadPool tp;
	tp.join(tp.add(test,i));

	//before commit f66048ed999aa1b50dc956c4a728ff565042d761
	cout<<i<<endl;	//0

	//since commit f66048ed999aa1b50dc956c4a728ff565042d761
	cout<<i<<endl;	//10
}

Contents

Introduction
Class view
Performance comparison
Compiler
How to compile
Compilation errors?
Tutorial
Future work

Introduction

This is a pure (which means it doesn't depend on any platform) and exception-safety C++ threadpool (so far, there is no standard threadpool in C++).
The goal of this project is to provide a fastest, beautiful and easy-to-use C++ threadpool library.

Class view

Two classes

CThreadPool, including the member function
	thread_id    add(Func &&,Args &&...)
	void         add_and_detach(Func &&,Args &&...)
	size_type    empty() const noexcept
	void         join(thread_id)
	void         join_all()
	bool         joinable(thread_id) const
	size_type    size() const noexcept
	void         wait_until_all_usable() const
	
CThreadPool_Ret, including the member function
	thread_id    add(Func &&,Args &&...)
	size_type    empty() const noexcept
	Ret          get(thread_id)
	size_type    size() const noexcept
	bool         valid(thread_id) const
	void         wait(thread_id) const
	void         wait_all() const

Use the CThreadPool_Ret when you want to get the return value of function.
Use the CThreadPool when you don't care the return value of function.
CThreadPool::add_and_detach is faster (very) than CThreadPool_Ret::add.

Performance comparison

progschj/ThreadPool, see Comparison.
Tyler-Hardin/thread_pool, see Comparison.
P.S. About bilash/threadpool, I don't want to test a C-like code.
P.S. nbsdx/ThreadPool cannot pass testing, see README.
P.S. philipphenkel/threadpool cannot pass testing, see README.
P.S. tghosgor/threadpool11 cannot pass testing, see README.
P.S. mtrebi/thread-pool cannot pass testing, see README.
See the directory for more details.

Compiler

Visual Studio 2017 15.5.5
g++ 7.2.1
clang++ 5.0.1

How to compile

You have to download my lib first.
The directory should be look like

├── lib
│   ├── header
│   ├── LICENSE
│   ├── README.md
│   ├── src
│   └── tutorial
└── ThreadPool
    ├── comparison
    ├── header
    ├── LICENSE
    ├── README.md
    ├── src
    └── tutorial

Don't forget to compile lib/src/Scope_guard.cpp.

Compilation errors?

See How to compile or email me

Tutorial

I provide example.cpp and example_ret.cpp to help you understand how to use this powerful thread pool
To use example.cpp:

g++ -std=c++17 tutorial/example.cpp src/* ../lib/src/Scope_guard.cpp

To use example_ret.cpp:

g++ -std=c++17 tutorial/example_ret.cpp src/IThreadPoolItemBase.cpp ../lib/src/Scope_guard.cpp

Future work

add a non-block version of CThreadPool::add
work stealing

Issues
  • Help wanted - call for member function of class

    Help wanted - call for member function of class

    @Fdhvdu I have an object in my code and i want to call a member function of this object by

    add_and_detach

    it is not possible to call i got this error error: invalid use of non-static member function

    is it possible to use this library inside the object to call the member function of the class ? this is important because i can not define this async function outside the class or as public, it uses the all private members that initialized and many other objects inside it.

    Regards,

    opened by mohsenomidi 2
  • thread pool max threads

    thread pool max threads

    @Fdhvdu

    i want to know if i defined the pool with max size 10 and some part of my code based on receiving requests add_and_detach the thread to this pool once the threadpool max size exceeds what will happen ?

    i need to know the excess threads will wait to finished the current number of threads and once the working threads release/finished, the waited threads will execute ? or all the new threads fired immediately without controlling on the max size of pool ?

    opened by mohsenomidi 1
  • abnormal library behavior on threads

    abnormal library behavior on threads

    @Fdhvdu i just installed and execute your sample for thread pooling. after execution stage 1 (same result on other usage in all 3 remaining stages) i see the result below that it is not correct :

    stage 1 add_and_detach - thread 4 wait 0 sec add_and_detach - thread 4 wait 0 sec add_and_detach - thread 4 wait 1 sec add_and_detach - thread 4 wait 3 sec

    as you see the all thread shows number 4, as their thread no, that it is not correct! you send the i as a variable to the function add_and_detach_func but it seems it is not working correctly

            for(size_t i{0};i!=tp.size();++i)
                    tp.add_and_detach(add_and_detach_func,i);
    
            std::size_t add_and_detach_func(const std::size_t i)
            {
                    using namespace std;
                    const auto sec{get()};
                    this_thread::sleep_for(chrono::seconds{sec});
                    lock_guard<mutex> lock{mut};
                    cout<<"add_and_detach - thread "<<i<<" wait "<<sec<<" sec"<<endl;
                    return i;
            }
    

    more info :

    Compiler : g++ (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0 OS: Ubuntu 18.04.1 LTS (GNU/Linux 4.15.0-54-generic x86_64)

    opened by mohsenomidi 1
  • Can't compile since file name changed from CScope_guard.cpp to Scope_guard.cpp

    Can't compile since file name changed from CScope_guard.cpp to Scope_guard.cpp

    download the lastest code. and compile, getting following error.

    [email protected]:~/threadpool/ThreadPool$ g++ -std=c++17 tutorial/example.cpp src/* ../lib/src/Scope_guard.cpp 
    In file included from src/../../lib/header/thread/CWait_bounded_queue.hpp:6:0,
                     from src/CThreadPool.cpp:6:
    src/../../lib/header/thread/CLock_bounded_queue.hpp:10:9: fatal error: ../tool/CScopeGuard.hpp: No such file or directory
     #include"../tool/CScopeGuard.hpp"
             ^~~~~~~~~~~~~~~~~~~~~~~~~
    compilation terminated.
    In file included from src/../../lib/header/thread/CWait_bounded_queue.hpp:6:0,
                     from src/IThreadPoolItemExecutor.cpp:6:
    src/../../lib/header/thread/CLock_bounded_queue.hpp:10:9: fatal error: ../tool/CScopeGuard.hpp: No such file or directory
     #include"../tool/CScopeGuard.hpp"
             ^~~~~~~~~~~~~~~~~~~~~~~~~
    compilation terminated.
    

    try to rename Scope_guard.cpp to CScope_guard.cpp didn't get any better.

    [email protected]:~/threadpool/ThreadPool$ g++ -std=c++17 tutorial/example.cpp src/* ../lib/src/CScope_guard.cpp 
    In file included from src/../../lib/header/thread/CWait_bounded_queue.hpp:6:0,
                     from src/CThreadPool.cpp:6:
    src/../../lib/header/thread/CLock_bounded_queue.hpp:10:9: fatal error: ../tool/CScopeGuard.hpp: No such file or directory
     #include"../tool/CScopeGuard.hpp"
             ^~~~~~~~~~~~~~~~~~~~~~~~~
    compilation terminated.
    In file included from src/../../lib/header/thread/CWait_bounded_queue.hpp:6:0,
                     from src/IThreadPoolItemExecutor.cpp:6:
    src/../../lib/header/thread/CLock_bounded_queue.hpp:10:9: fatal error: ../tool/CScopeGuard.hpp: No such file or directory
     #include"../tool/CScopeGuard.hpp"
             ^~~~~~~~~~~~~~~~~~~~~~~~~
    compilation terminated.
    

    I am trying to compile the example. thanks.

    opened by WanpengQian 1
  • Need your take on CTPL implementation

    Need your take on CTPL implementation

    Hello there,

    I'm developing a software which going to process heavy network traffics. (so many short-lived network sessions) And I have difficulty to select a good threadpool library for my use-case.

    My first attempt was to use CTPL (https://github.com/vit-vit/CTPL), which provides non-blocking queue (from Boost - with no lib dependency). From performance perspective I didn't get good result, as matter of fact, when i compare the threadpool performance vs creating the threads on-fly , I get better result from creating threads over and over again !

    So first, as you published your benchmark versus other libraries. I'd be appreciated if you could include CTPL into your benchmark tests.

    Also, in other posts, I found your working on your library for improvement. Is there any time-line for next release ?

    Thanks.

    enhancement 
    opened by atari83 0
  • Work stealing

    Work stealing

    Thank you for writing and publishing this thread pool implementation. It seems like a glaring hole in the C++ STL / Boost etc. As it stands std::async is totally unusable (runs "deferred" or one thread per task and blows up). C++20 offers "new futures", but no task launching/scheduling as far as I can see.

    So a pool is totally necessary!

    I noticed that your ReadMe says "TODO work stealing". Does you pool take this Sean Parent talk into account?

    https://www.youtube.com/watch?v=zULU6Hhp42w&feature=emb_logo

    This blog has a minimal implementation which does:

    https://vorbrodt.blog/2019/02/27/advanced-thread-pool/ https://github.com/mvorbrodt/blog

    enhancement 
    opened by oschonrock 4
  • Get stuck when running example with threadpool 1024

    Get stuck when running example with threadpool 1024

    change example.cpp pool size to 1024

    using namespace std;
    	nThread::CThreadPool tp{1024};
    	queue<nThread::CThreadPool::thread_id> que;
    

    and running the test. get stuck as following output

    add - thread 752 wait 3 sec
    add_and_detach - thread 1024 wait 1 sec
    add - thread 762 wait 0 sec
    add_and_detach - thread 1024 wait 2 sec
    add_and_detach - thread 1024 wait 3 sec
    add - thread 758 wait 2 sec
    add_and_detach - thread 1024 wait 1 sec
    add - thread 759 wait 2 sec
    add_and_detach - thread 1024 wait 2 sec
    add - thread 755 wait 3 sec
    add_and_detach - thread 1024 wait 3 sec
    add - thread 757 wait 3 sec
    add - thread 763 wait 1 sec
    add - thread 760 wait 3 sec
    add_and_detach - thread 1024 wait 3 sec
    add - thread 767 wait 1 sec
    add - thread 764 wait 2 sec
    add - thread 765 wait 2 sec
    add - thread 768 wait 1 sec
    add - thread 766 wait 3 sec
    
    help wanted 
    opened by WanpengQian 5
Owner
Han-Kuan Chen
Han-Kuan Chen
Thread pool - Thread pool using std::* primitives from C++17, with optional priority queue/greenthreading for POSIX.

thread_pool Thread pool using std::* primitives from C++11. Also includes a class for a priority thread pool. Requires concepts and C++17, including c

Tyler Hardin 75 Jul 13, 2022
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 614 Aug 8, 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 533 Jul 17, 2022
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
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.7k Aug 16, 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 Feb 14, 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 16 Jul 16, 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, http://en.wikipedia.org/wiki/

null 1.1k Aug 8, 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 838 Aug 11, 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 30, 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
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
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 104 Aug 5, 2022
C++14 asynchronous allocation aware futures (supporting then, exception handling, coroutines and connections)

Continuable is a C++14 library that provides full support for: lazy async continuation chaining based on callbacks (then) and expression templates, ca

Denis Blank 757 Aug 5, 2022
Smart queue that executes tasks in threadpool-like manner

execq execq is kind of task-based approach of processing data using threadpool idea with extended features. It supports different task sources and mai

Vladimir (Alkenso) 32 May 24, 2022
Light, fast, threadpool for C++20

riften::Thiefpool A blazing-fast, lightweight, work-stealing thread-pool for C++20. Built on the lock-free concurrent riften::Deque. Usage #include "r

Conor Williams 61 Jul 6, 2022
DwThreadPool - A simple, header-only, dependency-free, C++ 11 based ThreadPool library.

dwThreadPool A simple, header-only, dependency-free, C++ 11 based ThreadPool library. Features C++ 11 Minimal Source Code Header-only No external depe

Dihara Wijetunga 26 May 29, 2022