
On Fri, Oct 25, 2013 at 12:43 PM, Oliver Kowalke
2013/10/25 Giovanni Piero Deretta
that's the tricky part. To adjust the reference count, you must load and dereference the pointer itself, but between those two operations another thread might come in, replace the pointer, adjust the original pointer count down and free the pointed object, together with the counter. You need some way to defer destruction till a safe point (RCU, hazard pointers, etc).
what about this
// use-counter in T is an atomic too array< atomic< intrusive_ptr< T > >, 100 > a;
intrusive_ptr< T > p(...); intrusive_ptr_add_ref( p.get() ); // add an intrusive_ptr to the array if ( a[index].compare_exchange_strong(null_ptr,p) ) { // successful added }
// remove an interusive_ptr from array intrusive_ptr< T > e; // points to nothing if ( ! a[index].compare_exchange_strong(e, null_ptr) ) { // successfull removed intrusive_ptr_release( e.get() ); }
Nice! If these produce and consume are the only two operations on the array, from a cursory look, it should work fine. For the producer, a simple store with release semantics might actually be enough. The reason these operations should work is that they are actually move operations and do not change the count itself. What you can't do is copy a value out of the array leaving the original in place. -- gpd