get_id.pass.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. // id get_id() const;
  13. #include <thread>
  14. #include <new>
  15. #include <cstdlib>
  16. #include <cassert>
  17. #include "test_macros.h"
  18. class G
  19. {
  20. int alive_;
  21. public:
  22. static int n_alive;
  23. static bool op_run;
  24. G() : alive_(1) {++n_alive;}
  25. G(const G& g) : alive_(g.alive_) {++n_alive;}
  26. ~G() {alive_ = 0; --n_alive;}
  27. void operator()()
  28. {
  29. assert(alive_ == 1);
  30. assert(n_alive >= 1);
  31. op_run = true;
  32. }
  33. };
  34. int G::n_alive = 0;
  35. bool G::op_run = false;
  36. int main(int, char**)
  37. {
  38. {
  39. G g;
  40. std::thread t0(g);
  41. std::thread::id id0 = t0.get_id();
  42. std::thread t1;
  43. std::thread::id id1 = t1.get_id();
  44. assert(t0.get_id() == id0);
  45. assert(id0 != id1);
  46. assert(t1.get_id() == std::thread::id());
  47. t0.join();
  48. }
  49. return 0;
  50. }