set_value_at_thread_exit_void_pass.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is dual licensed under the MIT and the University of Illinois Open
  6. // Source Licenses. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. // Copyright (C) 2011 Vicente J. Botet Escriba
  10. //
  11. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  12. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  13. // <boost/thread/future.hpp>
  14. // class promise<R>
  15. // void promise<void>::set_value_at_thread_exit();
  16. #define BOOST_THREAD_VERSION 4
  17. #include <boost/thread/future.hpp>
  18. #include <boost/detail/lightweight_test.hpp>
  19. int i = 0;
  20. boost::promise<void> p;
  21. void func()
  22. {
  23. p.set_value_at_thread_exit();
  24. i = 1;
  25. }
  26. //void func2_mv(BOOST_THREAD_RV_REF(boost::promise<void>) p2)
  27. void func2_mv(boost::promise<void> p2)
  28. {
  29. p2.set_value_at_thread_exit();
  30. i = 2;
  31. }
  32. void func2(boost::promise<void> *p2)
  33. {
  34. p2->set_value_at_thread_exit();
  35. i = 2;
  36. }
  37. int main()
  38. {
  39. try
  40. {
  41. boost::future<void> f = p.get_future();
  42. boost::thread(func).detach();
  43. f.get();
  44. BOOST_TEST(i == 1);
  45. }
  46. catch(std::exception& )
  47. {
  48. BOOST_TEST(false);
  49. }
  50. catch(...)
  51. {
  52. BOOST_TEST(false);
  53. }
  54. try
  55. {
  56. boost::promise<void> p2;
  57. boost::future<void> f = p2.get_future();
  58. p = boost::move(p2);
  59. boost::thread(func).detach();
  60. f.get();
  61. BOOST_TEST(i == 1);
  62. }
  63. catch(std::exception& ex)
  64. {
  65. std::cout << __FILE__ << ":" << __LINE__ << " " << ex.what() << std::endl;
  66. BOOST_TEST(false);
  67. }
  68. catch(...)
  69. {
  70. BOOST_TEST(false);
  71. }
  72. try
  73. {
  74. boost::promise<void> p2;
  75. boost::future<void> f = p2.get_future();
  76. #if defined BOOST_THREAD_PROVIDES_VARIADIC_THREAD
  77. boost::thread(func2_mv, boost::move(p2)).detach();
  78. #else
  79. boost::thread(func2, &p2).detach();
  80. #endif
  81. f.wait();
  82. f.get();
  83. BOOST_TEST(i == 2);
  84. }
  85. catch(std::exception& ex)
  86. {
  87. std::cout << __FILE__ << ":" << __LINE__ << " " << ex.what() << std::endl;
  88. BOOST_TEST(false);
  89. }
  90. catch(...)
  91. {
  92. BOOST_TEST(false);
  93. }
  94. return boost::report_errors();
  95. }