0

There is a global variable called "pTrackerArray", which is used in Loki's SetLongevity function.

Declaration of pTrackerArray:

typedef std::list<LifetimeTracker*> TrackerArray;
extern LOKI_EXPORT TrackerArray* pTrackerArray;

Definition of SetLongevity:

template <typename T, typename Destroyer>
void SetLongevity(T* pDynObject, unsigned int longevity, Destroyer d)
{
    using namespace Private;

    // manage lifetime of stack manually
    if(pTrackerArray==0)
        pTrackerArray = new TrackerArray;

    // For simplicity, the rest of code is omitted
    ...
}

Is it thread safe to use pTrackerArray as such in SetLongevity?

4

1 回答 1

1

As shown, obviously not. However, if I am reading the rest of that file correctly, SetLongevity is, ultimately, only ever called from within a function that is itself properly wrapped in a mutex [provided you requested that the singleton be thread-safe, obviously]. So while that particular function has issues, its use is still perfectly safe.

However, the mutex they create in that base function is paramaterized on the type of singleton you are creating, while that global pointer is shared between all singletons. So yes, it does appear as though two different Singleton objects in two different threads could both access that function at once, causing havok.

于 2011-07-22T04:17:20.697 回答