allpix::ThreadPool

Pool of threads where event tasks can be submitted to.

#include <ThreadPool.hpp>

Public Classes

Name
class SafeQueue
Internal thread-safe queuing system.

Public Functions

Name
ThreadPool(const ThreadPool & rhs) =delete
Copying the thread pool is not allowed.
ThreadPool & operator=(const ThreadPool & rhs) =delete
Copying the thread pool is not allowed.
ThreadPool(unsigned int num_threads, unsigned int max_queue_size, const std::function< void()> & worker_init_function =nullptr, const std::function< void()> & worker_finalize_function =nullptr)
Construct thread pool with provided number of threads without buffered jobs.
ThreadPool(unsigned int num_threads, unsigned int max_queue_size, unsigned int max_buffered_size, const std::function< void()> & worker_init_function =nullptr, const std::function< void()> & worker_finalize_function =nullptr)
Construct thread pool with provided number of threads with buffered jobs.
template <typename Func ,typename… Args>
auto
submit(Func && func, Args &&… args)
Submit a standard job to be run by the thread pool. In case no workers are registered, the function will be executed immediately.
template <typename Func ,typename… Args>
auto
submit(uint64_t n, Func && func, Args &&… args)
Submit a priority job to be run by the thread pool. In case no workers are registered, the function will be executed immediately.
void markComplete(uint64_t n)
Mark identifier as completed.
uint64_t minimumUncompleted() const
Get the lowest ID that is not completely processed yet.
size_t queueSize() const
Return the total number of enqueued jobs.
size_t bufferedQueueSize() const
Return the number of jobs in buffered priority queue.
void checkException()
Check if any worker thread has thrown an exception.
void wait()
Waits for the worker threads to finish.
void destroy()
Invalidate all queues and joins all running threads when the pool is destroyed.
bool valid()
Returns if the threadpool is in a valid state (has not been invalidated)
~ThreadPool()
Destroy and wait for all threads to finish on destruction.
unsigned int threadNum()
Get the unique number of the current thread between 0 and the total number of threads.
unsigned int threadCount()
Get the number of thread including the main thread (thus always at least one)
void registerThreadCount(unsigned int cnt)
Add number to the total count of threads that could be used.

Public Functions Documentation

function ThreadPool

ThreadPool(
    const ThreadPool & rhs
) =delete

Copying the thread pool is not allowed.

function operator=

ThreadPool & operator=(
    const ThreadPool & rhs
) =delete

Copying the thread pool is not allowed.

function ThreadPool

ThreadPool(
    unsigned int num_threads,
    unsigned int max_queue_size,
    const std::function< void()> & worker_init_function =nullptr,
    const std::function< void()> & worker_finalize_function =nullptr
)

Construct thread pool with provided number of threads without buffered jobs.

Parameters:

  • num_threads Number of threads in the pool
  • max_queue_size Maximum size of the standard job queue
  • worker_init_function Function run by all the workers to initialize
  • worker_finalize_function Function run by all the workers to cleanup

Warning: Total count of threads need to be preregistered via ThreadPool::registerThreadCount

The threads are created in an exception-safe way and all of them will be destroyed when creation of one fails

function ThreadPool

ThreadPool(
    unsigned int num_threads,
    unsigned int max_queue_size,
    unsigned int max_buffered_size,
    const std::function< void()> & worker_init_function =nullptr,
    const std::function< void()> & worker_finalize_function =nullptr
)

Construct thread pool with provided number of threads with buffered jobs.

Parameters:

  • num_threads Number of threads in the pool
  • max_queue_size Maximum size of the standard job queue
  • max_buffered_size Maximum size of the buffered job queue (should be at least number of threads)
  • worker_init_function Function run by all the workers to initialize
  • worker_finalize_function Function run by all the workers to cleanup

Warning: Total count of threads need to be preregistered via ThreadPool::registerThreadCount

function submit

template <typename Func ,
typename... Args>
auto submit(
    Func && func,
    Args &&... args
)

Submit a standard job to be run by the thread pool. In case no workers are registered, the function will be executed immediately.

Parameters:

  • func Function to execute by the pool
  • args Parameters to pass to the function

function submit

template <typename Func ,
typename... Args>
auto submit(
    uint64_t n,
    Func && func,
    Args &&... args
)

Submit a priority job to be run by the thread pool. In case no workers are registered, the function will be executed immediately.

Parameters:

  • n Priority identifier or UINT64_MAX for non-prioritized submission
  • func Function to execute by the pool
  • args Parameters to pass to the function

Warning: This function can only be called if thread pool was initialized with buffered jobs

function markComplete

void markComplete(
    uint64_t n
)

Mark identifier as completed.

Parameters:

  • n Identifier that is complete

function minimumUncompleted

inline uint64_t minimumUncompleted() const

Get the lowest ID that is not completely processed yet.

Return: n Identifier that is not yet completed

function queueSize

inline size_t queueSize() const

Return the total number of enqueued jobs.

Return: The number of enqueued jobs

function bufferedQueueSize

inline size_t bufferedQueueSize() const

Return the number of jobs in buffered priority queue.

Return: The number of enqueued jobs in the buffered queue

function checkException

void checkException()

Check if any worker thread has thrown an exception.

Exceptions:

function wait

void wait()

Waits for the worker threads to finish.

function destroy

void destroy()

Invalidate all queues and joins all running threads when the pool is destroyed.

function valid

bool valid()

Returns if the threadpool is in a valid state (has not been invalidated)

function ~ThreadPool

~ThreadPool()

Destroy and wait for all threads to finish on destruction.

function threadNum

static unsigned int threadNum()

Get the unique number of the current thread between 0 and the total number of threads.

Return: Number of the current thread

function threadCount

static unsigned int threadCount()

Get the number of thread including the main thread (thus always at least one)

Return: Total count of threads

function registerThreadCount

static void registerThreadCount(
    unsigned int cnt
)

Add number to the total count of threads that could be used.

Parameters:

  • cnt Additional number of thread to preregister

Updated on 2024-12-13 at 08:31:36 +0000