Hello all,
I've been wondering how boost::mutex plays in the static initialization
game.
I'm toying with a "plugin" system where plugins statically register with
a singleton plugin_manager using a helper struct like this:
template <typename PluginT>
struct register_plugin {
register_plugin () {
shared_ptr ptr(new PluginT);
plugin_manager::instance().register(ptr);
}
};
static register_plugin objects, specialized for each plugin type, are
constructed in an anonymous namespace to register the plugin at static
initialization time, like so:
namespace {
register_plugin rp;
}
plugin_manager::instance() looks like:
{
static plugin_manager* s_instance;
// s_mutex is a static member
{
boost::mutex::scoped_lock lock(s_mutex);
if (s_instance == 0) {
s_instance = new plugin_manager;
}
}
return *s_instance;
}
So far I've implemented two plugins in this application and everything
just works.
It seems to me there is a static initialization race between every
register_plugin object and plugin_manager::s_mutex.
Am I just lucky or is there some magic about mutexes in this regard?
I'm working on a Fedora 7 i386 system.
--
Pedro LamarĂ£o