1 //===-------------- thread_local_destruction_order.pass.cpp ---------------===// 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: c++03 10 // UNSUPPORTED: libcxxabi-no-threads 11 12 #include <cassert> 13 #include <thread> 14 15 int seq = 0; 16 17 class OrderChecker { 18 public: 19 explicit OrderChecker(int n) : n_{n} { } 20 21 ~OrderChecker() { 22 assert(seq++ == n_); 23 } 24 25 private: 26 int n_; 27 }; 28 29 template <int ID> 30 class CreatesThreadLocalInDestructor { 31 public: 32 ~CreatesThreadLocalInDestructor() { 33 thread_local OrderChecker checker{ID}; 34 } 35 }; 36 37 OrderChecker global{7}; 38 39 void thread_fn() { 40 static OrderChecker fn_static{5}; 41 thread_local CreatesThreadLocalInDestructor<2> creates_tl2; 42 thread_local OrderChecker fn_thread_local{1}; 43 thread_local CreatesThreadLocalInDestructor<0> creates_tl0; 44 } 45 46 int main(int, char**) { 47 static OrderChecker fn_static{6}; 48 49 std::thread{thread_fn}.join(); 50 assert(seq == 3); 51 52 thread_local OrderChecker fn_thread_local{4}; 53 thread_local CreatesThreadLocalInDestructor<3> creates_tl; 54 55 return 0; 56 } 57