Skip to content

Commit 2f17f12

Browse files
committed
add docs for vtr::thread_pool
1 parent bae777e commit 2f17f12

File tree

3 files changed

+29
-9
lines changed

3 files changed

+29
-9
lines changed

Diff for: doc/src/api/vtrutil/index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ VTRUTIL API
1111
container_utils
1212
logging
1313
geometry
14+
parallel
1415
other

Diff for: doc/src/api/vtrutil/parallel.rst

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=====
2+
Parallel
3+
=====
4+
5+
vtr_thread_pool
6+
-------------
7+
.. doxygenfile:: vtr_thread_pool.h
8+
:project: vtr
9+
:sections: briefdescription detaileddescription func innernamespace enum
10+
11+
.. doxygenclass:: vtr::thread_pool
12+
:project: vtr
13+
:members:

Diff for: libs/libvtrutil/src/vtr_thread_pool.h

+15-9
Original file line numberDiff line numberDiff line change
@@ -26,39 +26,42 @@ namespace vtr {
2626
*
2727
* Example usage:
2828
*
29-
* vtr::thread_pool pool(4);
29+
* ```
30+
* vtr::thread_pool pool(4); // 4 threads
3031
* pool.schedule_work([]{
3132
* // Task body
3233
* });
33-
* pool.wait_for_all(); // There's no API to wait for a single task
34+
* pool.wait_for_all(); // There's no API to wait for a single task
35+
* ```
3436
*/
3537
class thread_pool {
3638
private:
37-
/* Thread-local data */
39+
/** Thread-local data */
3840
struct ThreadData {
3941
std::thread thread;
40-
/* Per-thread task queue */
42+
/** Per-thread task queue */
4143
std::queue<std::function<void()>> task_queue;
4244

43-
/* Threads wait on cv for a stop signal or a new task
45+
/** Threads wait on cv for a stop signal or a new task
4446
* queue_mutex is required for condition variable */
4547
std::mutex queue_mutex;
4648
std::condition_variable cv;
4749
bool stop = false;
4850
};
4951

50-
/* Container for thread-local data */
52+
/** Container for thread-local data */
5153
std::vector<std::unique_ptr<ThreadData>> threads;
52-
/* Used for round-robin scheduling */
54+
/** Used for round-robin scheduling */
5355
std::atomic<size_t> next_thread{0};
54-
/* Used for wait_for_all */
56+
/** Used for wait_for_all */
5557
std::atomic<size_t> active_tasks{0};
5658

57-
/* Condition variable for wait_for_all */
59+
/** Condition variable for wait_for_all */
5860
std::mutex completion_mutex;
5961
std::condition_variable completion_cv;
6062

6163
public:
64+
/** Create a thread pool with \p thread_count threads. */
6265
thread_pool(size_t thread_count) {
6366
threads.reserve(thread_count);
6467

@@ -96,6 +99,7 @@ class thread_pool {
9699
}
97100
}
98101

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

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

0 commit comments

Comments
 (0)