diff options
author | Christian Biesinger <cbiesinger@google.com> | 2019-10-08 17:25:26 -0500 |
---|---|---|
committer | Christian Biesinger <cbiesinger@google.com> | 2019-10-08 17:36:15 -0500 |
commit | d13d16c4d8e976ebd00353e04ca8a761b8850a11 (patch) | |
tree | c5934e66287c895b75dddf3a29945797f1160193 | |
parent | 52300df201ed1a35eb15b2bc5eb441a1b7eec393 (diff) | |
download | binutils-gdb-users/cbiesinger/threadpool.tar.gz |
Switch to futuresusers/cbiesinger/threadpool
-rw-r--r-- | gdb/gdbsupport/parallel-for.h | 26 | ||||
-rw-r--r-- | gdb/gdbsupport/thread_pool.c | 2 | ||||
-rw-r--r-- | gdb/gdbsupport/thread_pool.h | 10 |
3 files changed, 16 insertions, 22 deletions
diff --git a/gdb/gdbsupport/parallel-for.h b/gdb/gdbsupport/parallel-for.h index 6f4235da363..11af33d61f1 100644 --- a/gdb/gdbsupport/parallel-for.h +++ b/gdb/gdbsupport/parallel-for.h @@ -68,9 +68,7 @@ parallel_for_each (RandomIt first, RandomIt last, RangeFunction callback) parallel_for_pool.start (n_threads); } - std::mutex mtx; - std::condition_variable cv; - int num_finished = 0; + std::future<void> futures[n_threads]; size_t n_elements = last - first; if (n_threads > 1 && 2 * n_threads <= n_elements) @@ -79,12 +77,9 @@ parallel_for_each (RandomIt first, RandomIt last, RangeFunction callback) for (int i = 0; i < n_threads; ++i) { RandomIt end = first + elts_per_thread; - parallel_for_pool.post_task ([&, first, end] () { - callback (first, end); - std::unique_lock<std::mutex> lck (mtx); - num_finished++; - cv.notify_all (); - }); + futures[i] = parallel_for_pool.post_task ([&, first, end] () { + callback (first, end); + }); first = end; } } @@ -94,15 +89,10 @@ parallel_for_each (RandomIt first, RandomIt last, RangeFunction callback) /* Process all the remaining elements in the main thread. */ callback (first, last); - if (n_threads) - { - for (;;) { - std::unique_lock<std::mutex> lck (mtx); - if (num_finished == n_threads) - break; - cv.wait (lck); - } - } +#ifdef CXX_STD_THREAD + for (size_t i = 0; i < n_threads; ++i) + futures[i].wait (); +#endif /* CXX_STD_THREAD */ } } diff --git a/gdb/gdbsupport/thread_pool.c b/gdb/gdbsupport/thread_pool.c index 03fef956df7..a78edeaef7a 100644 --- a/gdb/gdbsupport/thread_pool.c +++ b/gdb/gdbsupport/thread_pool.c @@ -40,7 +40,7 @@ thread_pool::thread_function () break; if (m_tasks.empty ()) continue; - t = m_tasks.front(); + t = std::move (m_tasks.front()); m_tasks.pop(); } t (); diff --git a/gdb/gdbsupport/thread_pool.h b/gdb/gdbsupport/thread_pool.h index 77760a95042..9717263309f 100644 --- a/gdb/gdbsupport/thread_pool.h +++ b/gdb/gdbsupport/thread_pool.h @@ -8,6 +8,7 @@ #include <atomic> #include <mutex> #include <condition_variable> +#include <future> namespace gdb { @@ -20,11 +21,14 @@ class thread_pool { void start(size_t num_threads); - typedef std::function<void ()> task; - void post_task(task t) { + typedef std::packaged_task<void()> task; + std::future<void> post_task(std::function<void ()> func) { + task t(func); + std::future<void> f = t.get_future(); std::lock_guard<std::mutex> guard (m_tasks_mutex); - m_tasks.push (t); + m_tasks.push (std::move (t)); m_tasks_cv.notify_one (); + return f; } private: |