The master lock is another singleton type class, albeit one with special privledges in my library. In a multi-threaded build, every translation unit that includes a header that uses my code library will attempt to initialize the master lock as one of the first things it does. This is done by the same mechanism that std::cout and friends get intitialized, an object is created in every translation unit that checks to see if the master lock is initialized, if it's not, then it intializes it, and then the rest of the objects in that translation unit may be initialized. This relies on the fact that objects within a translation unit have deterministic initialization order.
The code for it is pretty simple. Every header includes a file that has this code in it:
class MasterLockInitializer { public: MasterLockInitializer(); ~MasterLockInitializer(); private: MasterLockInitializer(const MasterLockInitializer &); MasterLockInitializer & operator=(const MasterLockInitializer &); }; namespace { // every translation unit gets a master lock initializer MasterLockInitializer mli; }
Then in one of the source files there's the moral equivalent of:
namespace { int load_count = 0; Mutex * the_master_mutex = 0; } Mutex & get_master_mutex() { return *the_master_mutex; } MasterLockInitializer::MasterLockInitializer() { if (load_count == 0) { the_master_mutex = new Mutex(); } load_count++; } MasterLockInitializer::~MasterLockInitializer() { load_count--; if (load_count == 0) { delete the_master_mutex; the_master_mutex = 0; } }
On some days I think this is overkill. On other days it seems overly fragile. Right now it works, and that's what's important.