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() ); }