aboutsummaryrefslogtreecommitdiff
path: root/gdbsupport/thread-pool.h
diff options
context:
space:
mode:
authorTom Tromey <tom@tromey.com>2021-06-13 12:46:28 -0600
committerTom Tromey <tom@tromey.com>2022-04-12 09:31:16 -0600
commitf4565e4c99e768d3bcf2998979528569a65d8417 (patch)
tree51d6e54a3ba0a495cdedd2977d30a748920ea3cd /gdbsupport/thread-pool.h
parent82d734f7a3b6f08813a9ad6272aa026779c88975 (diff)
downloadgdb-f4565e4c99e768d3bcf2998979528569a65d8417.zip
gdb-f4565e4c99e768d3bcf2998979528569a65d8417.tar.gz
gdb-f4565e4c99e768d3bcf2998979528569a65d8417.tar.bz2
Return vector of results from parallel_for_each
This changes gdb::parallel_for_each to return a vector of the results. However, if the passed-in function returns void, the return type remains 'void'. This functionality is used later, to parallelize the new indexer.
Diffstat (limited to 'gdbsupport/thread-pool.h')
-rw-r--r--gdbsupport/thread-pool.h23
1 files changed, 22 insertions, 1 deletions
diff --git a/gdbsupport/thread-pool.h b/gdbsupport/thread-pool.h
index 2672e4d..3243346 100644
--- a/gdbsupport/thread-pool.h
+++ b/gdbsupport/thread-pool.h
@@ -64,7 +64,24 @@ public:
/* Post a task to the thread pool. A future is returned, which can
be used to wait for the result. */
- std::future<void> post_task (std::function<void ()> &&func);
+ std::future<void> post_task (std::function<void ()> &&func)
+ {
+ std::packaged_task<void ()> task (std::move (func));
+ std::future<void> result = task.get_future ();
+ do_post_task (std::packaged_task<void ()> (std::move (task)));
+ return result;
+ }
+
+ /* Post a task to the thread pool. A future is returned, which can
+ be used to wait for the result. */
+ template<typename T>
+ std::future<T> post_task (std::function<T ()> &&func)
+ {
+ std::packaged_task<T ()> task (std::move (func));
+ std::future<T> result = task.get_future ();
+ do_post_task (std::packaged_task<void ()> (std::move (task)));
+ return result;
+ }
private:
@@ -74,6 +91,10 @@ private:
/* The callback for each worker thread. */
void thread_function ();
+ /* Post a task to the thread pool. A future is returned, which can
+ be used to wait for the result. */
+ void do_post_task (std::packaged_task<void ()> &&func);
+
/* The current thread count. */
size_t m_thread_count = 0;