Class ThreadPool
Handles a pool of concurrent std::threads.

Defined in <seqan/parallel.h>
Signature class ThreadPool;

Member Function Overview

Interface Function Overview

Detailed Description

This is a simple raii-wrapper class to manage a set of std::threads.

Member Functions Detail

ThreadPool::TThreadPool() = default; ThreadPool::TThreadPool(ThreadPool const &) = delete; ThreadPool::TThreadPool(ThreadPool &&) = delete;

The constructor.

Creates a new instance with an empty pool.

Data Races

If not stated otherwise, concurrent invocation is not guaranteed to be thread-safe.

ThreadPool::~ThreadPool()

Warning.

If threads cannot be joined (e.g. dead lock) the destructor will wait forever.

The destructor.

Safely destroys the thread pool instance by joining all registered threads. This is an implicit barrier for the owning thread.

Data Races

If not stated otherwise, concurrent invocation is not guaranteed to be thread-safe.

Interface Functions Detail

void join(pool);

Explicit barrier over the pool.

Parameters

pool The ThreadPool to be joined.

Allows the user to wait for all registered threads to be finished before the calling thread continues its execution.

Data Races

This function is not thread safe. Concurrent invocations of this function for the same pool might result in undefined behavior.

void setCpuAffinity(pool, cpu, scale);

Note.

This function uses pthread functions on the native thread handle and is only available for linux platforms. On all other platforms this function is a no-op.

Pins the spawned threads in a round-robin fashion.

Parameters

pool The ThreadPool to pin the threads for.
cpu The number of the first cpu to be pinned.
scale A scaling factor to

Iterates over all threads registered in the pool and pins each of them to a cpu in a round-robin fashion. Using cpu and scale the cpu ids for pinning the threads can be configured dynamically.

Possible implementation:

cpu * scale % std::thread::hardware_concurrency();

Data Races

This function is not thread safe. Concurrent invocations of this function for the same pool might result in undefined behavior.

void spawn(pool, callable, ...args);

Spawns a new thread and registers it in the thread pool.

Parameters

pool The ThreadPool to register the new spawned thread in.
callable A callable object (e.g. functor or function) that should be executed by the spawned thread.
args Any number of arguments passed to the callable object.

Emplaces the thread in the pool and associates the thread with the callable by passing the args to the callable instance.

Data Races

This function is not thread safe. Concurrent invocations of this function for the same pool might result in undefined behavior.