Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2006-11-10 13:20:43


Talbot, George wrote:

> Wow...this is great! I think I may try to code up a wrapper that
> privately inherits from shared_ptr and just provides the spinlock
> around
> the operations, as a proof-of-concept. I'm doing this on Linux on x86
> right now (32-bit). Should I use this spinlock implementation:
> http://en.wikipedia.org/wiki/Spinlock

To be on the safe side, you'll probably need something a bit more elaborate
than that:

for( int i = 0; i < 4; ++i )
    if( atomic_exchange( &spinlock, 1 ) == 0 ) return; // got spinlock

// we probably need to yield to the thread holding the lock

for( int i = 0; i < 32; ++i )
{
    if( atomic_exchange( &spinlock, 1 ) == 0 ) return; // got spinlock
    sched_yield();
}

// we might need to yield to a lower-priority thread holding the lock

for( ;; )
{
    if( atomic_exchange( &spinlock, 1 ) == 0 ) return; // got spinlock
    nanosleep( some-small-value );
}

where 4 and 32 are fudge factors. But the simple version will work for a
proof of concept.

On Linux, it seems easiest to just use pthread_spin_lock/unlock for the
prototype.


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