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
6#define BOOST_THREAD_VERSION 4
7
8#include <iostream>
9
10#define BOOST_THREAD_PROVIDES_FUTURE
11#define BOOST_THREAD_PROVIDES_EXECUTORS
12#define BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION
13
14#if __cplusplus >= 201103L
15#include <boost/thread/executors/loop_executor.hpp>
16#include <boost/thread/executors/serial_executor.hpp>
17#endif
18#include <boost/thread/thread.hpp>
19#include <boost/atomic.hpp>
20
21using namespace std;
22
23int main()
24{
25#if __cplusplus >= 201103L
26 static std::size_t const nWorks = 100000;
27 boost::atomic<unsigned> execCount(0u);
28 boost::loop_executor ex;
29
30 boost::thread t([&ex]()
31 {
32 ex.loop();
33 });
34
35 {
36 boost::serial_executor serial(ex);
37
38 for (size_t i = 0; i < nWorks; i++)
39 serial.submit(closure: [i, &execCount] {
40 //std::cout << i << ".";
41 ++execCount;
42 });
43
44 serial.close();
45 }
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 }
59
60 ex.close();
61
62 t.join();
63 std::cout << "end\n" << std::endl;
64#endif
65 return 0;
66}
67

source code of boost/libs/thread/test/test_11611.cpp