brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.0 KiB · a6b3a34 Raw
99 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8//9// UNSUPPORTED: no-threads10 11// <thread>12 13// class thread14 15// void detach();16 17#include <thread>18#include <atomic>19#include <system_error>20#include <cassert>21 22#include "make_test_thread.h"23#include "test_macros.h"24 25std::atomic_bool done(false);26 27class G28{29    int alive_;30    bool done_;31public:32    static int n_alive;33    static bool op_run;34 35    G() : alive_(1), done_(false)36    {37        ++n_alive;38    }39 40    G(const G& g) : alive_(g.alive_), done_(false)41    {42        ++n_alive;43    }44    ~G()45    {46        alive_ = 0;47        --n_alive;48        if (done_) done = true;49    }50 51    void operator()()52    {53        assert(alive_ == 1);54        assert(n_alive >= 1);55        op_run = true;56        done_ = true;57    }58};59 60int G::n_alive = 0;61bool G::op_run = false;62 63void foo() { done = true; }64 65int main(int, char**)66{67    {68        G g;69        std::thread t0 = support::make_test_thread(g);70        assert(t0.joinable());71        t0.detach();72        assert(!t0.joinable());73        while (!done) {}74        assert(G::op_run);75        assert(G::n_alive == 1);76    }77    assert(G::n_alive == 0);78    done = false;79#ifndef TEST_HAS_NO_EXCEPTIONS80    {81        std::thread t0 = support::make_test_thread(foo);82        assert(t0.joinable());83        t0.detach();84        assert(!t0.joinable());85        try {86            t0.detach();87        } catch (std::system_error const&) {88        }89        // Wait to make sure that the detached thread has started up.90        // Without this, we could exit main and start destructing global91        // resources that are needed when the thread starts up, while the92        // detached thread would start up only later.93        while (!done) {}94    }95#endif96 97  return 0;98}99