I’d like to use __thread modifier, as a substitution for thread_local from C++11, in g++. Unfortunately my local thread variable doesn’t have trivial constructor (it has to set the value of one integer components).
I think about using this kind of construction:
__thread MyVariable *var;
__thread MyVariable* MyClass::var = nullptr;
End every time I’d like to get an acces to var I check if it was allocated:
if(var == nullptr)
var = new MyVariable(42);
But I have no idea, how to free allocated in this way memory.
__threadstorage specifier can not handle objects with non-trivial constructors and destructors.For objects with non-trivial constructors and destructors you may like to use
boost::thread_specfic_ptr<>. When a thread terminatesboost::thread_specfic_ptr<>invokesdeleteon the corresponding thread-specific object.