2013/10/25 Andrey Semashev
That won't work for several reasons. First, as outlined before, you can't store intrusive_ptr in atomic<>. Assuming you store T* instead, then you can't use nullptr as the first argument because CAS accepts it as an lvalue reference. Next, the removal of the pointer is not correct because when CAS fails the pointer is just loaded from the atomic, so you release the pointer and the dangling pointer is left in the array. You have to load e first, then make a loop with CAS and only release if it succeeds and the loaded pointer is not NULL.
array< atomic< T * >, 100 > a{ atomic< T* >( 0) }; T * desired = new T(); T * expected = 0; if ( a[index].compare_exchange_strong( expected, desired) ) { // desired added } T * expected1 = 0; T * expected2 = 0; if ( ! a[index].compare_exchange_strong( expected1, expected2) ) { // expected1 is not NULL, expected1 removed == a[index] contains NULL } should be OK