c++ - Why is 'this' not volatile? -
having spent last few days debugging multi-threading 1 thread deleting object still in use realised issue have been far easier , quicker diagnose if have made 'this' volatile. have changed crash dump on system (symbian os) far more informative.
so, there reason why cannot be, or shouldn't be?
edit: there no safe way prevent or check scenario. correct 1 solution accessing of stale class pointers have global variable holds pointer, , functions called should statics use global variable replacement 'this'?
static tany* gglobalpointer = null; #define harness static_cast<csomeclass*>(gglobalpointer); class csomeclass : public cbase { public: static void dosomething(); private: int imember; }; void csomeclass::dosomething() { if (!harness) { return; } harness->imember = 0; }
so if thread deleted , nulled global pointer caught immediately.
one issue think if compiler cached value of harness instead of checking each time it's used.
this not variable, constant. can change object referenced this, can't change value of this. because constants never change, there no need mark them volatile.
Comments
Post a Comment