From bd6336c629841c6db3a6ca53a936d629d34db53b Mon Sep 17 00:00:00 2001 From: Daniel Wilhelm Date: Fri, 18 Apr 2014 17:15:16 +0200 Subject: 4.1 --- zen/thread.h | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 zen/thread.h (limited to 'zen/thread.h') diff --git a/zen/thread.h b/zen/thread.h new file mode 100644 index 00000000..4db1e613 --- /dev/null +++ b/zen/thread.h @@ -0,0 +1,92 @@ +// ************************************************************************** +// * This file is part of the FreeFileSync project. It is distributed under * +// * GNU General Public License: http://www.gnu.org/licenses/gpl.html * +// * Copyright (C) 2008-2011 ZenJu (zhnmju123 AT gmx.de) * +// ************************************************************************** + +#ifndef BOOST_THREAD_WRAP_H +#define BOOST_THREAD_WRAP_H + +//temporary solution until C++11 thread becomes fully available + +#ifdef __MINGW32__ +#pragma GCC diagnostic push + +#pragma GCC diagnostic ignored "-Wswitch-enum" +#pragma GCC diagnostic ignored "-Wstrict-aliasing" +//#pragma GCC diagnostic ignored "-Wno-attributes" +//#pragma GCC diagnostic ignored "-Wredundant-decls" +//#pragma GCC diagnostic ignored "-Wcast-align" +//#pragma GCC diagnostic ignored "-Wunused-value" +#endif + +#include + +#ifdef __MINGW32__ +#pragma GCC diagnostic pop +#endif + +namespace zen +{ +//until std::async is available: +/* +Example: + Zstring dirname = ... + auto ft = zen::async([=](){ return zen::dirExists(dirname); }); + if (ft.timed_wait(boost::posix_time::milliseconds(200)) && ft.get()) + //dir exising +*/ +template +auto async(Function fun) -> boost::unique_future; + +template +void wait_for_all_timed(InputIterator first, InputIterator last, const Duration& wait_duration); + + + + + + + + + + + + + + + + + + + + +//###################### implementation ###################### +template inline +auto async2(Function fun) -> boost::unique_future //workaround VS2010 bug: bool (*fun)(); decltype(fun()) == int! +{ + boost::packaged_task pt([=] { return fun(); }); + auto fut = pt.get_future(); + boost::thread(std::move(pt)); + return std::move(fut); +} + + +template inline auto async(Function fun) -> boost::unique_future { return async2(fun); } + + +template inline +void wait_for_all_timed(InputIterator first, InputIterator last, const Duration& wait_duration) +{ + const boost::system_time endTime = boost::get_system_time() + wait_duration; + while (first != last) + { + first->timed_wait_until(endTime); + if (boost::get_system_time() >= endTime) + return; + ++first; + } +} +} + +#endif //BOOST_THREAD_WRAP_H -- cgit