set_exception_at_thread_exit_pass.cpp 2.4 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::set_exception_at_thread_exit(exception_ptr p);
  16. #define BOOST_THREAD_VERSION 4
  17. #include <boost/thread/future.hpp>
  18. #include <boost/detail/lightweight_test.hpp>
  19. #include <boost/static_assert.hpp>
  20. namespace boost
  21. {
  22. template <typename T>
  23. struct wrap
  24. {
  25. wrap(T const& v) :
  26. value(v)
  27. {
  28. }
  29. T value;
  30. };
  31. template <typename T>
  32. exception_ptr make_exception_ptr(T v)
  33. {
  34. return copy_exception(wrap<T> (v));
  35. }
  36. }
  37. #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
  38. void func(boost::promise<int> p)
  39. #else
  40. boost::promise<int> p;
  41. void func()
  42. #endif
  43. {
  44. //p.set_exception(boost::make_exception_ptr(3));
  45. p.set_exception_at_thread_exit(boost::make_exception_ptr(3));
  46. }
  47. int main()
  48. {
  49. {
  50. typedef int T;
  51. #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
  52. boost::promise<T> p;
  53. boost::future<T> f = p.get_future();
  54. boost::thread(func, boost::move(p)).detach();
  55. #else
  56. boost::future<T> f = p.get_future();
  57. boost::thread(func).detach();
  58. #endif
  59. try
  60. {
  61. f.get();
  62. BOOST_TEST(false);
  63. }
  64. catch (boost::wrap<int> i)
  65. {
  66. BOOST_TEST(i.value == 3);
  67. }
  68. catch (...)
  69. {
  70. BOOST_TEST(false);
  71. }
  72. }
  73. {
  74. typedef int T;
  75. boost::promise<T> p2;
  76. boost::future<T> f = p2.get_future();
  77. #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
  78. boost::thread(func, boost::move(p2)).detach();
  79. #else
  80. p = boost::move(p2);
  81. boost::thread(func).detach();
  82. #endif
  83. try
  84. {
  85. f.get();
  86. BOOST_TEST(false);
  87. }
  88. catch (boost::wrap<int> i)
  89. {
  90. BOOST_TEST(i.value == 3);
  91. }
  92. catch (...)
  93. {
  94. BOOST_TEST(false);
  95. }
  96. }
  97. return boost::report_errors();
  98. }