Any questions and/or comments on smr-based reference counted pointers?
You could use the logic to create a simple atomic COW buffer like this:
// buffer is assumed to be an efficent thread-safe buffer...
typedef local_smr_ptr< buffer > local_bufptr_t;
typedef shared_smr_ptr< buffer > shared_bufptr_t;
// shared
static shared_bufptr_t g_buf( new buffer( "init" ) );
// COW /w CAS
local_bufptr_t cmp, xchg( new buffer );
do
{ cmp = g_buf;
cmp->copy_to( xchg );
xchg->append( " updated via. CAS" );
} while ( ! g_buf.cas( cmp, xchg ) );
// COW /w XCHG
local_bufptr_t xchg( new buffer ),
cmp( g_buf );
cmp->copy_to( xchg );
xchg->append( " updated via. XCHG" );
g_buf.xchg( xchg );
Or you could use the SMR pointer to create robust synchronization objects
that will never be destroyed if there any threads that have references to
them:
// mutex is assumed to be an efficent mutex...
typedef local_smr_ptr< mutex > local_mutexptr_t;
typedef shared_smr_ptr< mutex > shared_mutexptr_t;
// shared
static shared_mutexptr_t g_mutex( new mutex );
// Lock/Unlock
local_mutexptr_t l_mutex( g_mutex );
if ( ! l_mutex ) { return; }
l_mutex->lock();
// whatever
l_mutex->unlock();
The smr atomic pointer has an expensive (store/load) before the reference
count can even be incremented. So, its a fairly costly when you load a
shared pointer into a local pointer. You would not want to use this for
everything.