Memory barriers vs lock/unlock

Paolo Carlini pcarlini@suse.de
Tue Nov 8 13:57:00 GMT 2005


Hi,

in our simple port of boost_shared_ptr we have some naively puzzling
things like:

  void
  release() // nothrow
  {
    if (__gnu_cxx::__exchange_and_add(&_M_use_count, -1) == 1)
    {
      dispose();
      __glibcxx_mutex_lock(_M_mutex);
      __glibcxx_mutex_unlock(_M_mutex);
      weak_release();
    }
  }

I'm currently investigating that, and I'm not an expert of this area,
and I'd like to have some help. If I remember correctly some old
exchanges those "weird" empty lock/unlock stem from the need to add
memory barriers, nothing more. Therefore, I'm wondering whether we
wouldn't be best off using right away _GLIBCXX_READ_MEM_BARRIER and
_GLIBCXX_WRITE_MEM_BARRIER like, for instance, libsupc++/guard.cc is
already doing. There is a lot to learn from the latter file, more
generally, in my opinion...

Any quick insight from the gurus?

Paolo.



More information about the Libstdc++ mailing list