Class
ThreadPoolHandles a pool of concurrent std::threads.
Defined in | <seqan/parallel.h> |
---|---|
Signature |
class ThreadPool;
|
Member Function Overview
Interface Function Overview
-
void join(pool);
Explicit barrier over the pool. -
void setCpuAffinity(pool, cpu, scale);
Pins the spawned threads in a round-robin fashion. -
void spawn(pool, callable, ...args);
Spawns a new thread and registers it in the thread pool.
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;
Creates a new instance with an empty pool.
Data Races
ThreadPool::~ThreadPool()
If threads cannot be joined (e.g. dead lock) the destructor will wait forever.
Safely destroys the thread pool instance by joining all registered threads. This is an implicit barrier for the owning thread.
Data Races
Interface Functions Detail
void join(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);
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.
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);
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.