Skip to content

martelkr/threadpool

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

C++20 Thread Pool (header file only)

MIT license cmake Build clang Build profile Build cppcheck Build Coverage Status

About

This is a header file only implementation of a C++20 style thread pool.

Compilation has been tested with:

  • GCC 11.3.0 (GNU/Linux Ubuntu 22.04.1 LTS)
    • cmake 3.22.1
    • googletest 1.11.0-3
    • clang 14.0.0-1ubuntu1
  • Visual Studio Community 2022 17.4.4 (64-bit) (Windows 11)
    • cmake 3.26.0-rc1
    • googletest 1.13.0

Usage

Create a thread pool and add jobs to it to be performed by the running threads.

void printVal(void)
{
    static int val = 1;
    std::cout << "Thread job: " << val++ << std::endl;
}

auto main(int argc, char* argv[]) -> int 
{
    com::thread::Pool p(1);

    for (auto i = 0; i < 5; ++i)
    {
        p.add(printVal);
    }
    ...
}

Thread Safety

All methods of the thread pool are thread safe.

Installation

Use the ThreadPool.hpp file in your source tree and include it in the file that need to use it.

Run Unit Tests

Unit tests run with ctest:

ctest -VV -C debug

Contribute

All contributions are highly appreciated.