Static initialization of class instances is not thread safe. The code below is an example of what not to do:
extern int computesomething(); class cachedcomputation { public: cachedcomputation() { result = computesomething(); } int result; }; void usecached() { static cachedcomputation c;
However, will the code below be thread safe? (Ignoring the ugliness of the decision) When or why will it break?
extern int computesomething(); class cachedcomputation { public: cachedcomputation() { if(0==InterlockedCompareExchange(&mutex, 1, 0)) {
source share