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)
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;
}
}
/* 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 */
}
}
#include <atomic>
#include <mutex>
#include <condition_variable>
+#include <future>
namespace gdb {
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: