I cannot replicate results in Listing 4-7 with code in Listing 4-6. "Tracer" (line 17) is not recognized as an object when using "thread_local" keyword; however, "Tracer" is recognized when I use "static" keyword and I get the output in Listing 4-7. I added the "thread" header file, #include <thread>, and it still did not work. Both clang++ and g++ are up to date which should support threading. Help is appreciated.
The text was updated successfully, but these errors were encountered:
Correct. It compiles but the thread_local object (t2) is not created. 'Tracer' is highlighted in first line but not in second. Although it compiles, the expected output is Thread-local variable constructed which I do not see.
Hi @jpc0016, I think you've uncovered a deeper issue! The standard actually says that the thread_local object's lifetime starts, at latest, on first use. This gives the compiler the option to emit initialization code at any point beginning with the thread's lifetime and as late as first use. All that's to say that you could achieve the listing's output in the following way: