move.pass.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. // thread(thread&& t);
  12. #include <thread>
  13. #include <new>
  14. #include <cstdlib>
  15. #include <cassert>
  16. class G
  17. {
  18. int alive_;
  19. public:
  20. static int n_alive;
  21. static bool op_run;
  22. G() : alive_(1) {++n_alive;}
  23. G(const G& g) : alive_(g.alive_) {++n_alive;}
  24. ~G() {alive_ = 0; --n_alive;}
  25. void operator()()
  26. {
  27. assert(alive_ == 1);
  28. assert(n_alive == 1);
  29. op_run = true;
  30. }
  31. void operator()(int i, double j)
  32. {
  33. assert(alive_ == 1);
  34. assert(n_alive == 1);
  35. assert(i == 5);
  36. assert(j == 5.5);
  37. op_run = true;
  38. }
  39. };
  40. int G::n_alive = 0;
  41. bool G::op_run = false;
  42. int main()
  43. {
  44. #ifdef _LIBCPP_MOVE
  45. {
  46. assert(G::n_alive == 0);
  47. assert(!G::op_run);
  48. std::thread t0(G(), 5, 5.5);
  49. std::thread::id id = t0.get_id();
  50. std::thread t1 = std::move(t0);
  51. assert(t1.get_id() == id);
  52. assert(t0.get_id() == std::thread::id());
  53. t1.join();
  54. assert(G::n_alive == 0);
  55. assert(G::op_run);
  56. }
  57. #endif // _LIBCPP_MOVE
  58. }