Fw: [patch] Make std::tr1::shared_ptr thread-safe.

Peter Dimov pdimov@mmltd.net
Sat Apr 2 20:49:00 GMT 2005


Paolo Carlini wrote:
> Alexander... Which is the point of writing "broken" two or three times
> per hour? Testcases are needed and concrete help too. Immanuel Kant
> comes to my mind: ...

Kant aside for a moment ;-)

One possible way to attempt to isolate a case which fails would be to use a 
basic_string<> with an allocator A that zeroes the storage before 
deallocating it (these are sometimes used for security reasons). Then we 
need

basic_string<char, ..., A> s1( "string" );
basic_string<char, ..., A> s2( s1 );

inline void f( basic_string<char, ..., A> const & s )
{
    assert( s[0] == 's' );
}

void thread1()
{
    f( s1 );
    // destroy s1
}

void thread2()
{
    f( s2 );
    // destroy s2
}

The intermediate function is needed because the non-const op[] will unshare. 
The trick is to somehow force thread1 and thread2 to execute at exactly the 
same time... one approach that comes to mind is to have two arrays of 
strings and run the two threads in opposite directions, "hoping" for them to 
meet at the middle and for an assert to fire. 



More information about the Libstdc++ mailing list