set_value_at_thread_exit_const_pass.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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::set_value_at_thread_exit(const R& r);
  16. #define BOOST_THREAD_VERSION 4
  17. #include <boost/thread/future.hpp>
  18. #include <boost/detail/lightweight_test.hpp>
  19. #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
  20. void func(boost::promise<int> p)
  21. #else
  22. boost::promise<int> p;
  23. void func()
  24. #endif
  25. {
  26. const int i = 5;
  27. p.set_value_at_thread_exit(i);
  28. }
  29. int main()
  30. {
  31. {
  32. #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
  33. boost::promise<int> p;
  34. boost::future<int> f = p.get_future();
  35. boost::thread(func, boost::move(p)).detach();
  36. #else
  37. boost::future<int> f = p.get_future();
  38. boost::thread(func).detach();
  39. #endif
  40. try
  41. {
  42. BOOST_TEST(f.get() == 5);
  43. }
  44. catch (...)
  45. {
  46. BOOST_TEST(false);
  47. }
  48. }
  49. {
  50. #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
  51. #else
  52. boost::promise<int> p2;
  53. boost::future<int> f = p2.get_future();
  54. p = boost::move(p2);
  55. boost::thread(func).detach();
  56. BOOST_TEST(f.get() == 5);
  57. #endif
  58. }
  59. return boost::report_errors();
  60. }