Skip to content

Instantly share code, notes, and snippets.

@EricWF
Created September 7, 2016 00:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EricWF/3bb50d4f28b91aa28d2adefea0e94a0e to your computer and use it in GitHub Desktop.
Save EricWF/3bb50d4f28b91aa28d2adefea0e94a0e to your computer and use it in GitHub Desktop.
//===-------------- 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