Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
T

thread-pool

> 数据库
Open source

BS::thread_pool: a fast, lightweight, modern, and easy-to-use C++17 / C++20 / C++23 thread pool library

3.0K stars0 likes2 views
WebsiteGitHub

About

BS::thread_pool: a fast, lightweight, modern, and easy-to-use C++17 / C++20 / C++23 thread pool library

BS::thread_pool: a fast, lightweight, modern, and easy-to-use C++17 / C++20 / C++23 thread pool library

By Barak Shoshany
Email: [email protected]
Website: https://baraksh.com/
GitHub: https://github.com/bshoshany

This is the complete documentation for v5.1.0 of the library, released on 2026-01-03.

  • Introduction
    • Motivation
    • Overview of features
  • Getting started
    • Installing the library
    • Compiling and compatibility
    • Constructors
    • Getting and resetting the number of threads in the pool
  • Submitting tasks to the queue
    • Submitting tasks with no arguments and receiving a future
    • Submitting tasks with arguments and receiving a future
    • Detaching and waiting for tasks
    • Waiting for submitted or detached tasks with a timeout
    • Class member functions as tasks
  • Parallelizing loops
    • Automatic parallelization of loops
    • Optimizing the number of blocks
    • Common index types
    • Parallelizing loops without futures
    • Parallelizing individual indices vs. blocks
    • Loops with return values
    • Parallelizing sequences
    • More about BS::multi_future
    • Submitting tasks in bulk without a loop
  • Utility classes
    • Synchronizing printing to a stream with BS::synced_stream
  • Managing tasks
    • Monitoring the tasks
    • Purging tasks
    • Exception handling
    • Getting information about the current thread
    • Thread initialization functions
    • Thread cleanup functions
    • Passing task arguments by constant reference
  • Optional features
    • Enabling features
    • Setting task priority
    • Pausing the pool
    • Avoiding wait deadlocks
  • Native extensions
    • Enabling the native extensions
    • Setting thread priority
    • Setting thread affinity
    • Setting thread names
    • Setting process priority
    • Setting process affinity
    • Accessing native thread handles
  • Testing the library
    • Automated tests
    • Performance tests
    • Finding the version of the library
  • Importing the library as a C++20 module
    • Compiling the module
    • Compiling with compile_cpp.py using import BS.thread_pool
    • Compiling with Clang using import BS.thread_pool
    • Compiling with GCC using import BS.thread_pool
    • Compiling with MSVC using import BS.thread_pool
    • Compiling with CMake using import BS.thread_pool
  • Importing the C++23 Standard Library as a module
    • Enabling import std
    • Compiling with compile_cpp.py using import std
    • Compiling with Clang and LLVM libc++ using import std
    • Compiling with GCC and GNU libstdc++ using import std
    • Compiling with MSVC and Microsoft STL using import std
    • Compiling with CMake using import std
  • Installing the library using package managers
    • Installing using vcpkg
    • Installing using Meson
    • Installing using Conan
    • Installing using CMake with CPM
    • Installing using CMake with FetchContent
  • Complete library reference
    • The BS::thread_pool class template
    • Optional features and the template parameter
    • The BS::this_thread class
    • The native extensions
    • The BS::multi_future class
    • The BS::synced_stream class
    • The BS::version class
    • Diagnostic variables
    • All names exported by the C++20 module
  • Development tools
    • The compile_cpp.py script
    • Visual Studio Code tasks
  • About the project
    • Bug reports and feature requests
    • Contribution and pull request policy
    • Starring the repository
    • Acknowledgements
    • Copyright and citing
    • About the author
    • Learning more about C++
    • Other projects to check out

Introduction

Motivation

Multithreading is essential for modern high-performance computing. Since C++11, the C++ standard library has included built-in low-level multithreading support using constructs such as std::thread. However, std::thread creates a new thread each time it is called, which can have a significant performance overhead. Furthermore, it is possible to create more threads than the hardware can handle simultaneously, potentially resulting in a substantial slowdown.

The library presented here contains a C++ thread pool class, BS::thread_pool, which avoids these issues by creating a fixed pool of threads once and for all, and then continuously reusing the same threads to perform different tasks throughout the lifetime of the program. By default, the number of threads in the pool is equal to the maximum number of threads that the hardware can run in parallel.

The user submits tasks to be executed into a queue. Whenever a thread becomes available, it retrieves the next task from the queue and executes it. The pool optionally produces an std::future for each task, which allows the user to wait for the task to finish executing and/or obtain its eventual return value, if applicable. Threads and tasks are autonomously managed by the pool in the background, without requiring any input from the user aside from submitting the desired tasks.

The design of this library is guided by four important principles. First, compactness: the entire library consists of just one self-contained header file, with no other components or dependencies. Second, portability: the library only utilizes the C++ standard library, without relying on any compiler extensions or 3rd-party libraries, and is therefore compatible with any modern standards-conforming C++ compiler on any platform, as long as it supports C++17 or later. Third, ease of use: the library is extensively documented, and programmers of any level should be able to use it right out of the box.

The fourth and final guiding principle is performance: each and every line of code in this library was carefully designed with maximum performance in mind, and performance was tested and verified on a variety of compilers and platforms. Indeed, the library was originally designed for use in the author's own computationally-intensive scientific computing projects, running both on high-end desktop/laptop computers and high-performance computing nodes.

Among the available C++ thread pool libraries, BS::thread_pool occupies the crucial middle ground between small bare-bones thread pool classes that offer rudimentary functionality and are only suitable for simple programs, and very large libraries that offer many advanced features but consist of multiple components and dependencies and involve complex APIs that require a substantial time investment to learn. BS::thread_pool was designed for users who want a simple and lightweight header-only library that is easy to learn and use, and can be readily incorporated into existing or new projects, but do not want to compromise on performance or functionality.

Obtaining the library is quick and easy; it can be downloaded manually from the GitHub repository, or installed automatically using a variety of package managers and build systems. The library can be imported either as a traditional header-only library, or as a modern C++20 module. BS::thread_pool has undergone extensive testing on multiple platforms and is actively used by thousands of C++ developers worldwide for a wide range of applications, from scientific computing to game development.

Overview of features

  • Fast:
    • Built from scratch with maximum performance in mind.
    • Suitable for use in high-performance computing nodes with a very large number of CPU cores.
    • Reusing threads avoids the overhead of creating and destroying them for individual tasks.
    • A task queue ensures that there are never more tasks running in parallel than is allowed by the hardware.
    • All optional features can be selectively turned on to ensure minimal overhead.
  • Lightweight:
    • Single header file: simply #include "BS_thread_pool.hpp" and you're all set!
    • Header-only: no need to install or build the library.
    • Self-contained: no external requirements or dependencies.
    • Portable: uses only the C++ standard library, and works with any C++17-compliant compiler on any platform.
    • Only 536 lines of code, including all optional features and utility classes (excluding comments, blank lines, lines containing only a single brace, C++17/20 polyfills, and native extensions).
  • Modern:
    • Fully supports C++17, C++20, and C++23, taking advantage of the latest language features when available for maximum performance, reliabilit

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

C++concurrencycpluspluscplusplus-17cplusplus-20

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category数据库
PricingOpen source

> Related tools

P
PostgreSQL
功能强大的开源关系型数据库
R
Redis
内存数据结构存储,常用作缓存与队列
M
MySQL
广泛使用的开源关系型数据库