Skip to content

r-sitko/thread-pool

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

C++ thread pool library

Description

C++ thread pool library implementation.

Cloning repository

git clone --recursive https://github.com/rsitko92/thread-pool.git

Prerequisites

Prerequisites for running thread pool unit tests

Prerequisites for using thread pool in project with CMake

Running unit tests

Running unit tests with using Docker

Being in root directory of project run in terminal:

sudo test/buildAndRunTestsDocker.sh

Running unit tests without using Docker

Being in root directory of project run in terminal:

test/buildAndRunTests.sh

Using thread pool library in project with CMake

  1. Add thread pool project using ExternalProject_Add function in CMakeLists.txt file.

  2. Link libThreadPool.a static library to project target in CMakeLists.txt file.

  3. Include DefaultFixedThreadPool.hpp header file from inc directory in header/source file:

    #include "DefaultFixedThreadPool.hpp"
  4. Create thread pool instance:

    auto pThreadPool = thread_pool::DefaultFixedThreadPool::create();
  5. Enqueue task in thread pool:

    auto exampleTask = [](int x)
      {std::this_thread::sleep_for(std::chrono::seconds(2));
      std::cout << "Hello from worker thread" << std::endl;
      std::cout << "Passed argument value = " << x << std::endl;};
    auto future = pThreadPool->enqueue(exampleTask, 10);

    Notice 1: In this example also chrono and iostream headers must be included.

    Notice 2: You can enqueue tasks and its arguments as rvalue objects. Then there will be invoked move constructors if exists for these class of objects.

  6. Get result:

    future.get();