Re: [boost] boost::shared_ptr<>::compare_and_swap()--AmIinsaneforwanting this?

-----Original Message----- From: boost-bounces@lists.boost.org [mailto:boost-bounces@lists.boost.org] On Behalf Of Peter Dimov Sent: Friday, November 10, 2006 1:21 PM To: boost@lists.boost.org Subject: Re: [boost]boost::shared_ptr<>::compare_and_swap()-- AmIinsaneforwanting this?
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
OK...attached is a test version of atomic_shared.h that does implements this using pthread_spin_lock/pthread_spin_unlock. Let me know if it looks sane whatsoever. Now I just saw the post by Chris Thomasson--is there a way to write something like atomic_shared without the spinlocks? I'm looking for a class that's as easy to use as shared_ptr, but that is read/write thread-safe and supports compare_and_set/swap. I'm not worrying about the ABA problem just yet. I also do not have a good answer to the post by Sohail Somani about what the compiler will do with this in the future. Note that I implemented atomic_shared::compare_and_set(), not compare_and_swap(). -- George T. Talbot <gtalbot@locuspharma.com> the
prototype.
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
participants (1)
-
Talbot, George