<div dir="ltr"><div><br></div><div>Hi -</div><div><br></div><div>I'm using the clang 3.5 builds from <a href="http://llvm.org/apt/">http://llvm.org/apt/</a> on Ubuntu 14.04:</div><div><br></div><div>$clang --version</div>
<div><div>Ubuntu clang version 3.5-1ubuntu1 (trunk) (based on LLVM 3.5)</div><div>Target: x86_64-pc-linux-gnu</div><div>Thread model: posix</div></div><div><br></div><div>The following program calls the Logger destructor after calling the registered atexit handler, since the logger object was constructed before the call to atexit:</div>
<div><br></div><div><div>$ cat ./tsan_vs_atexit.cpp</div><div>#include <cstdio></div><div>#include <cstdlib></div><div><br></div><div>class Logger {</div><div>public:</div><div> Logger() {</div><div> std::printf("Logger ctor\n");</div>
<div> }</div><div><br></div><div> void log(const char* msg) {</div><div> std::printf("%s", msg);</div><div> }</div><div><br></div><div> ~Logger() {</div><div> std::printf("Logger dtor\n");</div>
<div> }</div><div>};</div><div><br></div><div>Logger logger;</div><div><br></div><div>void log_from_atexit() {</div><div> logger.log("In log_from_atexit\n");</div><div>}</div><div><br></div><div>int main(int argc, char* argv[]) {</div>
<div> std::atexit(log_from_atexit);</div><div> return EXIT_SUCCESS;</div><div>}</div><div><br></div><div>$ clang++ -std=c++03 -fPIE -pie ./tsan_vs_atexit.cpp</div><div>$ ./a.out</div><div>Logger ctor</div><div>In log_from_atexit</div>
<div>Logger dtor</div><div><br></div><div>However, if I enable the thread sanitizer, now the atexit call is sequenced after the call to destroy logger.</div><div><br></div><div>$ clang++ -std=c++03 -fPIE -pie -fsanitize=thread ./tsan_vs_atexit.cpp</div>
<div>$ ./a.out</div><div>Logger ctor</div><div>Logger dtor</div><div>In log_from_atexit</div></div><div><br></div><div>Unless I've misunderstood the C++03 language around std::atexit that ordering is incorrect.</div><div>
<br></div><div>A quick google search didn't find much. Is this a known issue, or is the ordering not specified in C++03 as I thought?</div><div><br></div><div>Thanks,</div><div>Andrew</div><div><br></div></div>