join.pass.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // UNSUPPORTED: libcpp-has-no-threads
  10. // <thread>
  11. // class thread
  12. // void join();
  13. #include <thread>
  14. #include <new>
  15. #include <cstdlib>
  16. #include <cassert>
  17. #include <system_error>
  18. #include "test_macros.h"
  19. class G
  20. {
  21. int alive_;
  22. public:
  23. static int n_alive;
  24. static bool op_run;
  25. G() : alive_(1) {++n_alive;}
  26. G(const G& g) : alive_(g.alive_) {++n_alive;}
  27. ~G() {alive_ = 0; --n_alive;}
  28. void operator()()
  29. {
  30. assert(alive_ == 1);
  31. assert(n_alive >= 1);
  32. op_run = true;
  33. }
  34. };
  35. int G::n_alive = 0;
  36. bool G::op_run = false;
  37. void foo() {}
  38. int main(int, char**)
  39. {
  40. {
  41. G g;
  42. std::thread t0(g);
  43. assert(t0.joinable());
  44. t0.join();
  45. assert(!t0.joinable());
  46. #ifndef TEST_HAS_NO_EXCEPTIONS
  47. try {
  48. t0.join();
  49. assert(false);
  50. } catch (std::system_error const&) {
  51. }
  52. #endif
  53. }
  54. #ifndef TEST_HAS_NO_EXCEPTIONS
  55. {
  56. std::thread t0(foo);
  57. t0.detach();
  58. try {
  59. t0.join();
  60. assert(false);
  61. } catch (std::system_error const&) {
  62. }
  63. }
  64. #endif
  65. return 0;
  66. }