Class SPMD_ThreadPool
Defined in File spmd_threadpool.h
Class Documentation
-
class SPMD_ThreadPool
Single-Program Multiple-Data (SPMD) thread pool.
A fixed set of worker threads repeatedly executes the same callable in “epochs”. Each epoch publishes one task and wakes all workers. Each worker calls the task with its own rank (thread index). The caller thread waits until all workers finish the current epoch.
This design avoids repeated thread creation/join overhead when the number of workers is stable.
Note
Unless otherwise stated, public member functions are not thread-safe with respect to concurrent calls from multiple external threads.
Public Functions
-
SPMD_ThreadPool() = default
-
explicit SPMD_ThreadPool(std::size_t n)
Constructor with a fixed number of worker threads.
-
SPMD_ThreadPool(const SPMD_ThreadPool&) = delete
-
SPMD_ThreadPool &operator=(const SPMD_ThreadPool&) = delete
-
~SPMD_ThreadPool()
-
inline std::size_t GetSize() const noexcept
-
void Resize(std::size_t n)
Resize the number of worker threads.
-
void Stop()
Stop workers and join threads.
Note
This method is safe to call multiple times.
-
template<class F>
inline void AssignTask(F &&task) Assign task to be run by all worker threads in the next epoch.
Warning
Do not call this method while another thread is running. This method does not guarantee thread safety.
-
void Run(std::size_t thread_idx)
Run the currently assigned task for the worker with the given index.
-
void WaitAll()
Wait until all workers have completed the current epoch.
-
SPMD_ThreadPool() = default