Skip to content

ThreadPoolC++ is a simple and easy to use C++ threadpool implementation

Notifications You must be signed in to change notification settings

JakubFornadel/ThreadPoolCpp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ThreadPoolC++

ThreadPoolC++ is a simple and easy to use C++ threadpool implementation. It is just simple working POC, which can adjusted according to the use case.

Keywords: threadpool, C++17, thread-safe, concurrent

Build instructions

Use git to clone the repository.

git clone https://github.com/JakubFornadel/ThreadPoolCpp.git
cd ThreadPoolCpp

A CMake configuration file is provided for multiplatform support.

mkdir build
cd build

cmake ../
make

# run example
./ThreadPoolExample

# go back to the project root
cd ..

Usage

#include <iostream>
#include <cstdlib>
#include <signal.h>
#include <chrono>

#include "ThreadPool.hpp"

bool stopProgram = false;

void stopListener(int sigNum) {
  std::cout << "Caught ctrl+c signal. Stop program." << std::endl;
  stopProgram = true;
};

int main() {
  std::cout << "ThreadPool example started" << std::endl;

  // Register ctrl+c signal and signal handler
  signal(SIGINT, stopListener);

  // Creates simple thread pool
  concurrent::ThreadPool<std::string> threadPool( [](const auto& taskData) { std::cout << "Processing task: " << taskData << std::endl; } );
  
  // Generate random tasks until ctrl+c signal received
  while (stopProgram == false) {
    threadPool.emplace("Hello world task");
    std::this_thread::sleep_for(std::chrono::milliseconds(50));
  }

  threadPool.stopProcessing();

  std::cout << "Program finished" << std::endl;
}

About

ThreadPoolC++ is a simple and easy to use C++ threadpool implementation

Resources

Stars

Watchers

Forks

Packages

No packages published