
"Michael Gopshtein"
Hi,
As I understand from documentation, once thread_specific_ptr is created, first call to get() should return NULL in all threads. Actually sometimes it doesn't happen. Consider the following scenario:
1) thread T1 initializes some global tss object (by doing "new" on global thread_specific_ptr<>*) 2) thread T2 tries to read the value with get(), and gets null. It sets a correct value (unsigned int 42) 3) thread T1 deletes tss object, and allocates a new one instead.
This is now undefined behaviour. If you destroy a thread_specific_ptr whilst any thread has a non-NULL value then you cannot rely on what happens subsequently with respect to any thread that tries to use that or a new thread_specific_ptr instance.
4) thread T2 tries to read the value from the new object
So I expected that in (4) thread T2 will get NULL again. Actually, when new object created in (3) is created in same memory location, T2 gets the original value in (4), and not NULL. When new pointer allocated in (3) is different, T2 gets NULL as expected (verified using "placement new" to control location of the tss object).
Is it a bug or inpropriate use pattern?
Inappropriate use pattern. Anthony -- Author of C++ Concurrency in Action | http://www.manning.com/williams just::thread C++0x thread library | http://www.stdthread.co.uk Just Software Solutions Ltd | http://www.justsoftwaresolutions.co.uk 15 Carrallack Mews, St Just, Cornwall, TR19 7UL, UK. Company No. 5478976