test_11611.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. #define BOOST_THREAD_VERSION 4
  6. #include <iostream>
  7. #define BOOST_THREAD_PROVIDES_FUTURE
  8. #define BOOST_THREAD_PROVIDES_EXECUTORS
  9. #define BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION
  10. #if __cplusplus >= 201103L
  11. #include <boost/thread/executors/loop_executor.hpp>
  12. #include <boost/thread/executors/serial_executor.hpp>
  13. #endif
  14. #include <boost/thread/thread.hpp>
  15. #include <boost/atomic.hpp>
  16. using namespace std;
  17. int main()
  18. {
  19. #if __cplusplus >= 201103L
  20. static std::size_t const nWorks = 100000;
  21. boost::atomic<unsigned> execCount(0u);
  22. boost::loop_executor ex;
  23. boost::thread t([&ex]()
  24. {
  25. ex.loop();
  26. });
  27. {
  28. boost::serial_executor serial(ex);
  29. for (size_t i = 0; i < nWorks; i++)
  30. serial.submit([i, &execCount] {
  31. //std::cout << i << ".";
  32. ++execCount;
  33. });
  34. serial.close();
  35. }
  36. unsigned const cnt = execCount.load();
  37. if (cnt != nWorks) {
  38. // Since the serial_executor is closed, all work should have been done,
  39. // even though the loop_executor ex is not.
  40. std::cerr << "Only " << cnt << " of " << nWorks << " works executed!\n";
  41. return 1;
  42. }
  43. if (ex.try_executing_one()) {
  44. std::cerr
  45. << "loop_executor::try_executing_one suceeded on closed executor!\n";
  46. return 1;
  47. }
  48. ex.close();
  49. t.join();
  50. std::cout << "end\n" << std::endl;
  51. #endif
  52. return 0;
  53. }