C++ (Qt) template <class F> auto add_task(F && task)->std::future<decltype(task())> { std::lock_guard<std::mutex> locker(m_mutex); ++m_task_count; return m_pool.add_task(std::bind(&wrapper_pool::wrapper_task, this, std::forward<F>(task))); }
C++ (Qt)class wrapper_pool{public: wrapper_pool(specmath::thread_pool & pool) : m_pool(pool), m_task_count(0) {} template <class F> auto add_task(F && task)->std::future<decltype (task())> { using R = decltype (task()); std::lock_guard<std::mutex> locker(m_mutex); ++m_task_count; return m_pool.add_task(std::bind(&wrapper_pool::wrapper_task<R>, this, std::forward<F>(task))); } void wait_for_all() { std::unique_lock<std::mutex> locker(m_mutex); m_cond.wait(locker, [&]() { return !m_task_count; }); } private: specmath::thread_pool & m_pool; std::atomic_int m_task_count; std::mutex m_mutex; std::condition_variable m_cond; template <class R> R wrapper_task(const std::function<R()> & task) { auto res = task(); { std::unique_lock<std::mutex> locker(m_mutex); if (--m_task_count == 0) { locker.unlock(); m_cond.notify_one(); } } return res; }};
C++ (Qt) template <class R> R wrapper_task(const std::function<R()> & task) { auto res = task(); { std::unique_lock<std::mutex> locker(m_mutex); if (--m_task_count == 0) { locker.unlock(); m_cond.notify_one(); } } return res; }
C++ (Qt) template <class R> R wrapper_task(const std::function<R()> & task) { auto res = task(); if (--m_task_count == 0) std::unique_lock<std::mutex> locker(m_mutex); if (m_task_count == 0) m_cond.notify_one(); } return res; }
C++ (Qt)const size_t WEIGHT_TASK = 64;const size_t NUM_TASKS = 1024 * 1000;const size_t NUM_PASSES = 8; #define M_AX 0
Bashpass 0with std::futures<double> result = -52.8008 total time (ms) = 5184with wait_for_all result = -156.814 total time (ms) = 5220 pass 1with std::futures<double> result = -66.3582 total time (ms) = 4875with wait_for_all result = -16.7101 total time (ms) = 5256 pass 2with std::futures<double> result = 209.773 total time (ms) = 4785with wait_for_all result = 137.421 total time (ms) = 5227 pass 3with std::futures<double> result = -109.539 total time (ms) = 4818with wait_for_all result = -311.828 total time (ms) = 5272 pass 4with std::futures<double> result = 64.4322 total time (ms) = 4721with wait_for_all result = -55.3244 total time (ms) = 5272 pass 5with std::futures<double> result = 141.65 total time (ms) = 4828with wait_for_all result = 272.018 total time (ms) = 5184 pass 6with std::futures<double> result = 7.6451 total time (ms) = 5081with wait_for_all result = -56.6884 total time (ms) = 5194 pass 7with std::futures<double> result = -171.047 total time (ms) = 4989with wait_for_all result = -130.11 total time (ms) = 5253 End
C++ (Qt)for (size_t i = 0; i < data.size(); ++i) { double val = dist(gen); data[i] = val * 2 - 1.0; }
C++ (Qt) for (size_t i = 0; i < data.size(); ++i) { data[i] = dist(gen); }
C++ (Qt)void wrapper_task(const std::function<void()> & task) { if (task) task(); #if M_AX std::unique_lock<std::mutex> locker(m_mutex); if (--m_task_count == 0) { locker.unlock(); m_cond.notify_one(); } #else if (std::atomic_fetch_sub(&m_task_count, 1) == 1) { std::unique_lock<std::mutex> locker(m_mutex); if (m_task_count == 0) { locker.unlock(); // <== делаем unlock перед пробудкой.. m_cond.notify_one(); } } #endif }