CellModules
ThreadPool Class Reference

Simple threadpool, largely inspired by github.com/progschj/ThreadPool. More...

#include <threadpool.hpp>

Public Member Functions

size_t getNbThreads ()
 
 ThreadPool (size_t nt)
 
void setNbThreads (size_t n)
 
void waitUntilLast ()
 
template<class F >
auto enqueue (F &&f)
 
template<class F >
auto enqueueWithFuture (F &&f)
 
template<typename Container , typename F >
void autoChunks (Container &v, size_t minChunkSize, double avgTasksPerThread, F f)
 
 ~ThreadPool ()
 

Private Attributes

size_t nthreads = 0
 
std::vector< std::thread > workers
 
std::queue< std::function< void()> > tasks
 
std::mutex queue_mutex
 
std::condition_variable condition
 
std::condition_variable waitingLast
 
bool stop = false
 
std::atomic< int > currentTasks
 

Detailed Description

Simple threadpool, largely inspired by github.com/progschj/ThreadPool.

Definition at line 24 of file threadpool.hpp.

Constructor & Destructor Documentation

◆ ThreadPool()

ThreadPool::ThreadPool ( size_t  nt)
inline

Definition at line 36 of file threadpool.hpp.

◆ ~ThreadPool()

ThreadPool::~ThreadPool ( )
inline

Definition at line 123 of file threadpool.hpp.

Member Function Documentation

◆ autoChunks()

template<typename Container , typename F >
void ThreadPool::autoChunks ( Container &  v,
size_t  minChunkSize,
double  avgTasksPerThread,
f 
)
inline

Definition at line 99 of file threadpool.hpp.

◆ enqueue()

template<class F >
auto ThreadPool::enqueue ( F &&  f)
inline

Definition at line 65 of file threadpool.hpp.

◆ enqueueWithFuture()

template<class F >
auto ThreadPool::enqueueWithFuture ( F &&  f)
inline

Definition at line 75 of file threadpool.hpp.

◆ getNbThreads()

size_t ThreadPool::getNbThreads ( )
inline

Definition at line 35 of file threadpool.hpp.

◆ setNbThreads()

void ThreadPool::setNbThreads ( size_t  n)
inline

Definition at line 38 of file threadpool.hpp.

◆ waitUntilLast()

void ThreadPool::waitUntilLast ( )
inline

Definition at line 59 of file threadpool.hpp.

Member Data Documentation

◆ condition

std::condition_variable ThreadPool::condition
private

Definition at line 30 of file threadpool.hpp.

◆ currentTasks

std::atomic<int> ThreadPool::currentTasks
private

Definition at line 32 of file threadpool.hpp.

◆ nthreads

size_t ThreadPool::nthreads = 0
private

Definition at line 26 of file threadpool.hpp.

◆ queue_mutex

std::mutex ThreadPool::queue_mutex
private

Definition at line 29 of file threadpool.hpp.

◆ stop

bool ThreadPool::stop = false
private

Definition at line 31 of file threadpool.hpp.

◆ tasks

std::queue<std::function<void()> > ThreadPool::tasks
private

Definition at line 28 of file threadpool.hpp.

◆ waitingLast

std::condition_variable ThreadPool::waitingLast
private

Definition at line 30 of file threadpool.hpp.

◆ workers

std::vector<std::thread> ThreadPool::workers
private

Definition at line 27 of file threadpool.hpp.


The documentation for this class was generated from the following file: