15a83710eSEric Fiselier //===----------------------------------------------------------------------===//
25a83710eSEric Fiselier //
357b08b09SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
457b08b09SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
557b08b09SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65a83710eSEric Fiselier //
75a83710eSEric Fiselier //===----------------------------------------------------------------------===//
85a83710eSEric Fiselier //
95a83710eSEric Fiselier // UNSUPPORTED: libcpp-has-no-threads
105a83710eSEric Fiselier 
115a83710eSEric Fiselier // <thread>
125a83710eSEric Fiselier 
135a83710eSEric Fiselier // class thread
145a83710eSEric Fiselier 
155a83710eSEric Fiselier // void join();
165a83710eSEric Fiselier 
175a83710eSEric Fiselier #include <thread>
185a83710eSEric Fiselier #include <new>
195a83710eSEric Fiselier #include <cstdlib>
205a83710eSEric Fiselier #include <cassert>
214efa1ad5SEric Fiselier #include <system_error>
224efa1ad5SEric Fiselier 
234efa1ad5SEric Fiselier #include "test_macros.h"
245a83710eSEric Fiselier 
255a83710eSEric Fiselier class G
265a83710eSEric Fiselier {
275a83710eSEric Fiselier     int alive_;
285a83710eSEric Fiselier public:
295a83710eSEric Fiselier     static int n_alive;
305a83710eSEric Fiselier     static bool op_run;
315a83710eSEric Fiselier 
325a83710eSEric Fiselier     G() : alive_(1) {++n_alive;}
335a83710eSEric Fiselier     G(const G& g) : alive_(g.alive_) {++n_alive;}
345a83710eSEric Fiselier     ~G() {alive_ = 0; --n_alive;}
355a83710eSEric Fiselier 
365a83710eSEric Fiselier     void operator()()
375a83710eSEric Fiselier     {
385a83710eSEric Fiselier         assert(alive_ == 1);
395a83710eSEric Fiselier         assert(n_alive >= 1);
405a83710eSEric Fiselier         op_run = true;
415a83710eSEric Fiselier     }
425a83710eSEric Fiselier };
435a83710eSEric Fiselier 
445a83710eSEric Fiselier int G::n_alive = 0;
455a83710eSEric Fiselier bool G::op_run = false;
465a83710eSEric Fiselier 
474efa1ad5SEric Fiselier void foo() {}
484efa1ad5SEric Fiselier 
49*2df59c50SJF Bastien int main(int, char**)
505a83710eSEric Fiselier {
515a83710eSEric Fiselier     {
529a37bc91SEric Fiselier         G g;
539a37bc91SEric Fiselier         std::thread t0(g);
545a83710eSEric Fiselier         assert(t0.joinable());
555a83710eSEric Fiselier         t0.join();
565a83710eSEric Fiselier         assert(!t0.joinable());
574efa1ad5SEric Fiselier #ifndef TEST_HAS_NO_EXCEPTIONS
584efa1ad5SEric Fiselier         try {
594efa1ad5SEric Fiselier             t0.join();
604efa1ad5SEric Fiselier             assert(false);
614efa1ad5SEric Fiselier         } catch (std::system_error const&) {
625a83710eSEric Fiselier         }
634efa1ad5SEric Fiselier #endif
644efa1ad5SEric Fiselier     }
654efa1ad5SEric Fiselier #ifndef TEST_HAS_NO_EXCEPTIONS
664efa1ad5SEric Fiselier     {
674efa1ad5SEric Fiselier         std::thread t0(foo);
684efa1ad5SEric Fiselier         t0.detach();
694efa1ad5SEric Fiselier         try {
704efa1ad5SEric Fiselier             t0.join();
714efa1ad5SEric Fiselier             assert(false);
724efa1ad5SEric Fiselier         } catch (std::system_error const&) {
734efa1ad5SEric Fiselier         }
744efa1ad5SEric Fiselier     }
754efa1ad5SEric Fiselier #endif
76*2df59c50SJF Bastien 
77*2df59c50SJF Bastien   return 0;
785a83710eSEric Fiselier }
79