Skip to content

Commit b3ef4d4

Browse files
committed
update thread_pool docs
1 parent cd95c56 commit b3ef4d4

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

libs/libvtrutil/src/vtr_thread_pool.h

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,6 @@
33
/**
44
* @file vtr_thread_pool.h
55
* @brief A generic thread pool for parallel task execution
6-
*
7-
* A thread pool for parallel task execution. It is a naive
8-
* implementation which uses a queue for each thread and assigns
9-
* tasks in a round robin fashion.
10-
*
11-
* Example usage:
12-
*
13-
* vtr::thread_pool pool(4);
14-
* pool.schedule_work([]{
15-
* // Task body
16-
* });
17-
* pool.wait_for_all(); // There's no API to wait for a single task
186
*/
197

208
#include <thread>
@@ -31,6 +19,21 @@
3119

3220
namespace vtr {
3321

22+
/**
23+
* A thread pool for parallel task execution. It is a naive
24+
* implementation which uses a queue for each thread and assigns
25+
* tasks in a round robin fashion.
26+
*
27+
* Example usage:
28+
*
29+
* ```
30+
* vtr::thread_pool pool(4); // 4 threads
31+
* pool.schedule_work([]{
32+
* // Task body
33+
* });
34+
* pool.wait_for_all(); // There's no API to wait for a single task
35+
* ```
36+
*/
3437
class thread_pool {
3538
private:
3639
/** Thread-local data */
@@ -58,6 +61,7 @@ class thread_pool {
5861
std::condition_variable completion_cv;
5962

6063
public:
64+
/** Create a thread pool with \p thread_count threads. */
6165
thread_pool(size_t thread_count) {
6266
threads.reserve(thread_count);
6367

@@ -95,6 +99,7 @@ class thread_pool {
9599
}
96100
}
97101

102+
/** Schedule a function to be executed on one of the threads. */
98103
template<typename F>
99104
void schedule_work(F&& f) {
100105
active_tasks++;
@@ -132,6 +137,8 @@ class thread_pool {
132137
thread_data->cv.notify_one();
133138
}
134139

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

0 commit comments

Comments
 (0)