Created
September 7, 2016 00:45
-
-
Save EricWF/3bb50d4f28b91aa28d2adefea0e94a0e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//===-------------- thread_local_destruction_order.pass.cpp ---------------===// | |
// | |
// The LLVM Compiler Infrastructure | |
// | |
// This file is dual licensed under the MIT and the University of Illinois Open | |
// Source Licenses. See LICENSE.TXT for details. | |
// | |
//===----------------------------------------------------------------------===// | |
// UNSUPPORTED: c++98, c++03 | |
#include <cassert> | |
#include <thread> | |
#include <iostream> | |
int seq = 0; | |
class OrderChecker { | |
public: | |
explicit OrderChecker(int n) : n_{n} { } | |
~OrderChecker() { | |
if (seq++ != n_) { | |
std::cout << "check for n_ failed: " << n_; | |
assert(false); | |
} | |
} | |
private: | |
int n_; | |
}; | |
template <int ID = 0> | |
class CreatesThreadLocalInDestructor { | |
public: | |
CreatesThreadLocalInDestructor(int n = ID) : n_{n} { } | |
~CreatesThreadLocalInDestructor() { | |
thread_local OrderChecker checker{n_}; | |
} | |
private: | |
int n_; | |
}; | |
OrderChecker global{7}; | |
void thread_fn() { | |
static OrderChecker fn_static{5}; | |
thread_local CreatesThreadLocalInDestructor<2> creates_tl2; | |
thread_local OrderChecker fn_thread_local{1}; | |
thread_local CreatesThreadLocalInDestructor<0> creates_tl; | |
} | |
int main() { | |
static OrderChecker fn_static{6}; | |
std::thread{thread_fn}.join(); | |
std::cout << seq << std::endl; | |
assert(seq == 3); | |
thread_local OrderChecker fn_thread_local{4}; | |
thread_local CreatesThreadLocalInDestructor<3> creates_tl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment