Boost logo

Boost Users :

From: Pedro Lamarão (pedro.lamarao_at_[hidden])
Date: 2007-11-05 19:55:15


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<plugin_base> 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<foo_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

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net