Home | History | Annotate | Download | only in thread.thread.member
      1 //===----------------------------------------------------------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is dual licensed under the MIT and the University of Illinois Open
      6 // Source Licenses. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // UNSUPPORTED: libcpp-has-no-threads
     11 
     12 // <thread>
     13 
     14 // class thread
     15 
     16 // void join();
     17 
     18 #include <thread>
     19 #include <new>
     20 #include <cstdlib>
     21 #include <cassert>
     22 #include <system_error>
     23 
     24 #include "test_macros.h"
     25 
     26 class G
     27 {
     28     int alive_;
     29 public:
     30     static int n_alive;
     31     static bool op_run;
     32 
     33     G() : alive_(1) {++n_alive;}
     34     G(const G& g) : alive_(g.alive_) {++n_alive;}
     35     ~G() {alive_ = 0; --n_alive;}
     36 
     37     void operator()()
     38     {
     39         assert(alive_ == 1);
     40         assert(n_alive >= 1);
     41         op_run = true;
     42     }
     43 };
     44 
     45 int G::n_alive = 0;
     46 bool G::op_run = false;
     47 
     48 void foo() {}
     49 
     50 int main()
     51 {
     52     {
     53         G g;
     54         std::thread t0(g);
     55         assert(t0.joinable());
     56         t0.join();
     57         assert(!t0.joinable());
     58 #ifndef TEST_HAS_NO_EXCEPTIONS
     59         try {
     60             t0.join();
     61             assert(false);
     62         } catch (std::system_error const&) {
     63         }
     64 #endif
     65     }
     66 #ifndef TEST_HAS_NO_EXCEPTIONS
     67     {
     68         std::thread t0(foo);
     69         t0.detach();
     70         try {
     71             t0.join();
     72             assert(false);
     73         } catch (std::system_error const&) {
     74         }
     75     }
     76 #endif
     77 }
     78