Boost logo

Boost :

Subject: Re: [boost] [smart_ptr] [thread] (was Re: A summary of testing withsanitizers)
From: Peter Dimov (lists_at_[hidden])
Date: 2015-04-24 16:42:20


Ben Pope wrote:
> On Tuesday, April 21, 2015 06:42 PM, Ben Pope wrote:
> > 89 SUMMARY: ThreadSanitizer: data race
> > /home/ben/development/boost/test/build/boost_root/status/../boost/smart_ptr/detail/shared_count.hpp:449:49
> > in boost::detail::shared_count::shared_count(boost::detail::shared_count
> > const&)
>
> I've just been looking at this one, ...

I took a quick look at all the smart_ptr issues, and all of them seem to be
in Boost.Thread; essentially ThreadSanitizer claims that Boost.Thread causes
a race on a shared_ptr variable (although the actual report varies.)

> ... essentially it seems to boil down to:
>
> #include <atomic>
> #include <thread>
> #include <cstdint>
>
> typedef _Atomic( std::int_least32_t ) atomic_int_least32_t;
>
> inline void atomic_increment( atomic_int_least32_t * pw )
> {
> __c11_atomic_fetch_add( pw, 1, __ATOMIC_RELAXED );
> }
>
> int main()
> {
> atomic_int_least32_t use_count_;
> std::thread t ([&](){ atomic_increment(&use_count_); });
> __c11_atomic_init( &use_count_, 1 );
> t.join();
> }

It's not possible to achieve this by using shared_ptr (without contortions),
as the constructor initializes use_count_ to 1 and you can't (ordinarily)
make a thread addref a shared_ptr variable that hasn't been constructed yet.

A likelier explanation would have been that ThreadSanitizer is in some way
confused by the way Boost.Thread creates threads, but in fact, stack traces
show pthread_create, so I've no idea.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk