From bcc5cc28c6dc5178e8f4fd0cc521034ae5def388 Mon Sep 17 00:00:00 2001 From: Daniel Wilhelm Date: Fri, 18 Apr 2014 17:22:18 +0200 Subject: 5.10 --- zen/thread.h | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) (limited to 'zen/thread.h') diff --git a/zen/thread.h b/zen/thread.h index 432a521e..f00c0298 100644 --- a/zen/thread.h +++ b/zen/thread.h @@ -9,7 +9,6 @@ //temporary solution until C++11 thread becomes fully available #include -#include "fixed_list.h" //fix this pathetic boost thread warning mess #ifdef __MINGW32__ @@ -64,12 +63,12 @@ public: bool timedWait(const Duration& duration) const; //true: "get()" is ready, false: time elapsed //return first value or none if all jobs failed; blocks until result is ready! - std::unique_ptr get() const; //must be called only once! + std::unique_ptr get() const; //may be called only once! private: class AsyncResult; - FixedList workload; //note: we cannot use std::vector: compiler error on GCC 4.7, probably a boost screw-up std::shared_ptr result; + size_t jobsTotal; }; @@ -93,11 +92,12 @@ private: #endif template inline -auto async2(Function fun) -> boost::unique_future //workaround VS2010 bug: bool (*fun)(); decltype(fun()) == int! +auto async2(Function fun) -> boost::unique_future //support for workaround of VS2010 bug: bool (*fun)(); decltype(fun()) == int! { - boost::packaged_task pt([=] { return fun(); }); + boost::packaged_task pt(fun); auto fut = pt.get_future(); - boost::thread(std::move(pt)); + boost::thread t(std::move(pt)); + t.detach(); //we have to be explicit since C++11: [thread.thread.destr] ~thread() calls std::terminate() if joinable()!!! return std::move(fut); //compiler error without "move", why needed??? } @@ -181,28 +181,27 @@ private: template inline -RunUntilFirstHit::RunUntilFirstHit() : result(std::make_shared()) {} +RunUntilFirstHit::RunUntilFirstHit() : result(std::make_shared()), jobsTotal(0) {} template template inline void RunUntilFirstHit::addJob(Fun f) //f must return a std::unique_ptr containing a value on success { - auto result2 = result; //VC11: this is ridiculous!!! - workload.emplace_back([result2, f] - { - result2->reportFinished(f()); - }); + auto result2 = result; //MSVC2010: this is ridiculous!!! + boost::thread t([result2, f] { result2->reportFinished(f()); }); + ++jobsTotal; + t.detach(); //we have to be explicit since C++11: [thread.thread.destr] ~thread() calls std::terminate() if joinable()!!! } template template inline -bool RunUntilFirstHit::timedWait(const Duration& duration) const { return result->waitForResult(workload.size(), duration); } +bool RunUntilFirstHit::timedWait(const Duration& duration) const { return result->waitForResult(jobsTotal, duration); } template inline -std::unique_ptr RunUntilFirstHit::get() const { return result->getResult(workload.size()); } +std::unique_ptr RunUntilFirstHit::get() const { return result->getResult(jobsTotal); } } #endif //BOOST_THREAD_WRAP_H -- cgit