Skip to content

add docs for vtr::thread_pool #2968

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/src/api/vtrutil/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ VTRUTIL API
container_utils
logging
geometry
parallel
other
13 changes: 13 additions & 0 deletions doc/src/api/vtrutil/parallel.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
=====
Parallel
=====

vtr_thread_pool
-------------
.. doxygenfile:: vtr_thread_pool.h
:project: vtr
:sections: briefdescription detaileddescription func innernamespace enum

.. doxygenclass:: vtr::thread_pool
:project: vtr
:members:
24 changes: 15 additions & 9 deletions libs/libvtrutil/src/vtr_thread_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,39 +26,42 @@ namespace vtr {
*
* Example usage:
*
* vtr::thread_pool pool(4);
* ```
* vtr::thread_pool pool(4); // 4 threads
* pool.schedule_work([]{
* // Task body
* });
* pool.wait_for_all(); // There's no API to wait for a single task
* pool.wait_for_all(); // There's no API to wait for a single task
* ```
*/
class thread_pool {
private:
/* Thread-local data */
/** Thread-local data */
struct ThreadData {
std::thread thread;
/* Per-thread task queue */
/** Per-thread task queue */
std::queue<std::function<void()>> task_queue;

/* Threads wait on cv for a stop signal or a new task
/** Threads wait on cv for a stop signal or a new task
* queue_mutex is required for condition variable */
std::mutex queue_mutex;
std::condition_variable cv;
bool stop = false;
};

/* Container for thread-local data */
/** Container for thread-local data */
std::vector<std::unique_ptr<ThreadData>> threads;
/* Used for round-robin scheduling */
/** Used for round-robin scheduling */
std::atomic<size_t> next_thread{0};
/* Used for wait_for_all */
/** Used for wait_for_all */
std::atomic<size_t> active_tasks{0};

/* Condition variable for wait_for_all */
/** Condition variable for wait_for_all */
std::mutex completion_mutex;
std::condition_variable completion_cv;

public:
/** Create a thread pool with \p thread_count threads. */
thread_pool(size_t thread_count) {
threads.reserve(thread_count);

Expand Down Expand Up @@ -96,6 +99,7 @@ class thread_pool {
}
}

/** Schedule a function to be executed on one of the threads. */
template<typename F>
void schedule_work(F&& f) {
active_tasks++;
Expand Down Expand Up @@ -133,6 +137,8 @@ class thread_pool {
thread_data->cv.notify_one();
}

/** Wait until the work queue is empty.
* Note that functions are allowed to schedule new functions. */
void wait_for_all() {
std::unique_lock<std::mutex> lock(completion_mutex);
completion_cv.wait(lock, [this]() { return active_tasks == 0; });
Expand Down