thread_pool.h#
-
namespace ONNX_LIGHT_NAMESPACE
-
namespace utils
-
class ThreadPool#
- #include <thread_pool.h>
Thread pool used to parallelize delayed block loading.
Manages a fixed set of worker threads that pull jobs from a shared queue. Call Start() to launch workers, SubmitTask() to enqueue work, and Wait() to block until all submitted jobs have completed and the workers have stopped. The pool can be restarted by calling Start() again after Wait() returns. Clear() resets internal state when the pool is idle (not started).
Public Functions
-
ThreadPool()#
Initializes the thread pool in a stopped, idle state.
-
~ThreadPool()#
Destroys the thread pool by calling Wait().
Blocks until all pending jobs have finished executing, then stops and joins all worker threads. If the pool has already been stopped, this is a no-op.
-
void Start(int32_t num_threads)#
Starts the pool by launching worker threads.
- Parameters:
num_threads – Number of worker threads to create. Any negative value (for example
-1) is treated as a request to use the value returned bystd::thread::hardware_concurrency().
-
void SubmitTask(std::function<void()> &&job)#
Submits a callable job for asynchronous execution.
If no worker threads have been started, the job is queued and will be run inline when Wait() is called.
- Parameters:
job – Callable to execute.
-
void Wait()#
Blocks until all submitted jobs have finished executing, then stops and joins all worker threads.
If no workers were started, runs any queued jobs inline on the calling thread. After Wait() returns, the pool is in a stopped state and can be restarted with Start().
-
inline size_t GetThreadCount() const#
Returns the number of worker threads currently in the pool.
-
inline bool IsStarted() const#
Returns whether the pool has been started and workers are running.
-
void Clear()#
Resets the pool to an empty, idle state.
Clears the worker list and any pending jobs. Must only be called when the pool is not started (i.e., after Wait() has returned or before Start() has been called).
Private Functions
-
void worker_thread()#
Entry point executed by each worker thread.
-
ThreadPool()#
-
class ThreadPool#
-
namespace utils