Boost logo

Boost :

Subject: Re: [boost] [shared_ptr][thread] spinlock initialization
From: Peter Dimov (lists_at_[hidden])
Date: 2012-09-27 08:24:36


Yi Ding wrote:
> I started using the spinlock in boost/smart_ptr/detail/spinlock.hpp
> and discovered that it needs to be manually initialized, even for the
> spinlock_sync version, which became a nasty bug when it manifested
> itself. This seems a bit out of sync with the rest of the mutexes in
> boost::thread.
>
> Now, I'm guessing that the fact that the spinlock is in boost::detail
> means that it's not really for outside consumption (newbie boost user
> here), but is there something in the works that will be?
>
> In the meantime, would it be possible to automatically initialize the
> spinlock (trivial in the case of spinlock_sync) or are there
> architectural issues with that?

The spinlock is a POD, initialized with BOOST_DETAIL_SPINLOCK_INIT, so that
the initialization is static and not dynamic, which is intended to prevent
initialization order issues.

If you want a dynamically initialized spinlock, you can create your own
using boost::detail::spinlock as a basis, something like

static boost::detail::spinlock initializer = BOOST_DETAIL_SPINLOCK_INIT;

class my_spinlock
{
private:

    boost::detail::spinlock sl_;

    my_spinlock( my_spinlock const& );
    my_spinlock & operator=( my_spinlock const& );

public:

    my_spinlock(): sl_( initializer ) {}

// lock, unlock, try_lock, scoped_lock
};


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk