summaryrefslogtreecommitdiff
path: root/zen/thread.h
blob: 4db1e6131aa2fcc45926cd67e9f08628456865e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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 <boost/thread.hpp>

#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 <class Function>
auto async(Function fun) -> boost::unique_future<decltype(fun())>;

template<class InputIterator, class Duration>
void wait_for_all_timed(InputIterator first, InputIterator last, const Duration& wait_duration);




















//###################### implementation ######################
template <class T, class Function> inline
auto async2(Function fun) -> boost::unique_future<T> //workaround VS2010 bug: bool (*fun)();  decltype(fun()) == int!
{
    boost::packaged_task<T> pt([=] { return fun(); });
    auto fut = pt.get_future();
    boost::thread(std::move(pt));
    return std::move(fut);
}


template <class Function> inline auto async(Function fun) -> boost::unique_future<decltype(fun())> { return async2<decltype(fun())>(fun); }


template<class InputIterator, class Duration> 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
bgstack15