https://gcc.gnu.org/bugzilla/show_bug.cgi?id=118854

--- Comment #14 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Jonathan Wakely from comment #9)
> (In reply to Andrew Pinski from comment #6)
> > Libc++ is NOT a variation or fork of libstc++ but it's own implementation
> > and unrelated to libstdc++. If you hit there too, 99% chances were it is a
> > bug in your code.
> 
> It's unclear if he's using -stdlib=libc++ or -stdlib=libstdc++ so it might
> be using libstdc++ after all, even with Clang.

The same Asan error *does* happen with -stdlib=libc++ though.

> The fix is to join the background thread before pInstance is destroyed,
> which can be done by ensuring that pInstance is constructed *before*
> gBackgroundThread, so that it's not destroyed until *after*
> gBackgroundThread.

Simply swapping the order of the two globals is sufficient to fix it:

std::shared_ptr< Resource >
Resource::
_pInstance = std::make_shared< Resource >();

BackgroundThread gBackgroundThread;


This way the gBackgroundThread destructor happens before the pInstance
destructor, so the thread is joined before the shared_ptr is destroyed, so
there's no data race.

Reply via email to