http://gcc.gnu.org/bugzilla/show_bug.cgi?id=46455
--- Comment #7 from Jonathan Wakely <redi at gcc dot gnu.org> 2010-11-15
12:26:24 UTC ---
Could you try this, which is a simplified version of the shared_ptr refcounting
code and should have the same behaviour:
#include <ext/atomicity.h>
#include <ext/concurrence.h>
#include <assert.h>
static int count0 = 0;
struct X
{
X() : count1(1), count2(1) { ++count0; }
~X() { assert( count1==0 ); assert( count2==0 ); --count0; }
void release()
{
if (__gnu_cxx::__exchange_and_add_dispatch(&count1, -1) == 1)
{
if (__gnu_cxx::__exchange_and_add_dispatch(&count2, -1) == 1)
delete this;
}
}
__gnu_cxx::__mutex m;
_Atomic_word count1;
_Atomic_word count2;
};
int main()
{
for (int i=0; i<100; ++i)
{
X* x = new X;
x->release();
}
assert( count0 == 0 );
}