F.pass.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. // <thread>
  10. // class thread
  11. // template <class F, class ...Args> thread(F&& f, Args&&... args);
  12. #include <thread>
  13. #include <new>
  14. #include <cstdlib>
  15. #include <cassert>
  16. unsigned throw_one = 0xFFFF;
  17. void* operator new(std::size_t s) throw(std::bad_alloc)
  18. {
  19. if (throw_one == 0)
  20. throw std::bad_alloc();
  21. --throw_one;
  22. return std::malloc(s);
  23. }
  24. void operator delete(void* p) throw()
  25. {
  26. std::free(p);
  27. }
  28. bool f_run = false;
  29. void f()
  30. {
  31. f_run = true;
  32. }
  33. class G
  34. {
  35. int alive_;
  36. public:
  37. static int n_alive;
  38. static bool op_run;
  39. G() : alive_(1) {++n_alive;}
  40. G(const G& g) : alive_(g.alive_) {++n_alive;}
  41. ~G() {alive_ = 0; --n_alive;}
  42. void operator()()
  43. {
  44. assert(alive_ == 1);
  45. assert(n_alive >= 1);
  46. op_run = true;
  47. }
  48. void operator()(int i, double j)
  49. {
  50. assert(alive_ == 1);
  51. assert(n_alive >= 1);
  52. assert(i == 5);
  53. assert(j == 5.5);
  54. op_run = true;
  55. }
  56. };
  57. int G::n_alive = 0;
  58. bool G::op_run = false;
  59. int main()
  60. {
  61. {
  62. std::thread t(f);
  63. t.join();
  64. assert(f_run == true);
  65. }
  66. f_run = false;
  67. {
  68. try
  69. {
  70. throw_one = 0;
  71. std::thread t(f);
  72. assert(false);
  73. }
  74. catch (...)
  75. {
  76. throw_one = 0xFFFF;
  77. assert(!f_run);
  78. }
  79. }
  80. {
  81. assert(G::n_alive == 0);
  82. assert(!G::op_run);
  83. std::thread t((G()));
  84. t.join();
  85. assert(G::n_alive == 0);
  86. assert(G::op_run);
  87. }
  88. G::op_run = false;
  89. {
  90. try
  91. {
  92. throw_one = 0;
  93. assert(G::n_alive == 0);
  94. assert(!G::op_run);
  95. std::thread t((G()));
  96. assert(false);
  97. }
  98. catch (...)
  99. {
  100. throw_one = 0xFFFF;
  101. assert(G::n_alive == 0);
  102. assert(!G::op_run);
  103. }
  104. }
  105. #ifndef _LIBCPP_HAS_NO_VARIADICS
  106. {
  107. assert(G::n_alive == 0);
  108. assert(!G::op_run);
  109. std::thread t(G(), 5, 5.5);
  110. t.join();
  111. assert(G::n_alive == 0);
  112. assert(G::op_run);
  113. }
  114. #endif // _LIBCPP_HAS_NO_VARIADICS
  115. }