]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // Copyright (C) 2014 Vicente Botet |
2 | // | |
3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | |
4 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
5 | ||
b32b8144 | 6 | #define BOOST_THREAD_VERSION 4 |
7c673cae FG |
7 | |
8 | #include <iostream> | |
7c673cae FG |
9 | |
10 | #define BOOST_THREAD_PROVIDES_FUTURE | |
11 | #define BOOST_THREAD_PROVIDES_EXECUTORS | |
12 | #define BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION | |
b32b8144 FG |
13 | |
14 | #if __cplusplus >= 201103L | |
7c673cae | 15 | #include <boost/thread/executors/loop_executor.hpp> |
7c673cae | 16 | #include <boost/thread/executors/serial_executor.hpp> |
b32b8144 | 17 | #endif |
7c673cae | 18 | #include <boost/thread/thread.hpp> |
b32b8144 | 19 | #include <boost/atomic.hpp> |
7c673cae FG |
20 | |
21 | using namespace std; | |
22 | ||
23 | int main() | |
24 | { | |
b32b8144 FG |
25 | #if __cplusplus >= 201103L |
26 | static std::size_t const nWorks = 100000; | |
27 | boost::atomic<unsigned> execCount(0u); | |
7c673cae FG |
28 | boost::loop_executor ex; |
29 | ||
7c673cae FG |
30 | boost::thread t([&ex]() |
31 | { | |
32 | ex.loop(); | |
33 | }); | |
34 | ||
35 | { | |
7c673cae FG |
36 | boost::serial_executor serial(ex); |
37 | ||
b32b8144 FG |
38 | for (size_t i = 0; i < nWorks; i++) |
39 | serial.submit([i, &execCount] { | |
7c673cae | 40 | //std::cout << i << "."; |
b32b8144 | 41 | ++execCount; |
7c673cae FG |
42 | }); |
43 | ||
44 | serial.close(); | |
45 | } | |
b32b8144 FG |
46 | unsigned const cnt = execCount.load(); |
47 | if (cnt != nWorks) { | |
48 | // Since the serial_executor is closed, all work should have been done, | |
49 | // even though the loop_executor ex is not. | |
50 | std::cerr << "Only " << cnt << " of " << nWorks << " works executed!\n"; | |
51 | return 1; | |
52 | } | |
53 | ||
54 | if (ex.try_executing_one()) { | |
55 | std::cerr | |
56 | << "loop_executor::try_executing_one suceeded on closed executor!\n"; | |
57 | return 1; | |
58 | } | |
7c673cae FG |
59 | |
60 | ex.close(); | |
61 | ||
62 | t.join(); | |
b32b8144 FG |
63 | std::cout << "end\n" << std::endl; |
64 | #endif | |
7c673cae FG |
65 | return 0; |
66 | } |