detach.pass.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 detach();
  13. #include <thread>
  14. #include <atomic>
  15. #include <system_error>
  16. #include <cassert>
  17. #include "test_macros.h"
  18. std::atomic_bool done(false);
  19. class G
  20. {
  21. int alive_;
  22. bool done_;
  23. public:
  24. static int n_alive;
  25. static bool op_run;
  26. G() : alive_(1), done_(false)
  27. {
  28. ++n_alive;
  29. }
  30. G(const G& g) : alive_(g.alive_), done_(false)
  31. {
  32. ++n_alive;
  33. }
  34. ~G()
  35. {
  36. alive_ = 0;
  37. --n_alive;
  38. if (done_) done = true;
  39. }
  40. void operator()()
  41. {
  42. assert(alive_ == 1);
  43. assert(n_alive >= 1);
  44. op_run = true;
  45. done_ = true;
  46. }
  47. };
  48. int G::n_alive = 0;
  49. bool G::op_run = false;
  50. void foo() {}
  51. int main(int, char**)
  52. {
  53. {
  54. G g;
  55. std::thread t0(g);
  56. assert(t0.joinable());
  57. t0.detach();
  58. assert(!t0.joinable());
  59. while (!done) {}
  60. assert(G::op_run);
  61. assert(G::n_alive == 1);
  62. }
  63. assert(G::n_alive == 0);
  64. #ifndef TEST_HAS_NO_EXCEPTIONS
  65. {
  66. std::thread t0(foo);
  67. assert(t0.joinable());
  68. t0.detach();
  69. assert(!t0.joinable());
  70. try {
  71. t0.detach();
  72. } catch (std::system_error const&) {
  73. }
  74. }
  75. #endif
  76. return 0;
  77. }