thread() noexcept = default;
#ifdef _GLIBCXX_HAS_GTHREADS
+ private:
+ // This adds to user code that creates std:thread objects (because
+ // it is called by the template ctor below) strong references to
+ // pthread_create and pthread_join, which ensures they are both
+ // linked in even during static linking. We can't depend on
+ // gthread calls to bring them in, because those may use weak
+ // references.
+ static void
+ _M_thread_deps_never_run() {
+#ifdef GTHR_ACTIVE_PROXY
+ reinterpret_cast<void (*)(void)>(&pthread_create)();
+ reinterpret_cast<void (*)(void)>(&pthread_join)();
+#endif
+ }
+
+ public:
template<typename _Callable, typename... _Args,
typename = _Require<__not_same<_Callable>>>
explicit
"std::thread arguments must be invocable after conversion to rvalues"
);
-#ifdef GTHR_ACTIVE_PROXY
- // Create a reference to pthread_create, not just the gthr weak symbol.
- auto __depend = reinterpret_cast<void(*)()>(&pthread_create);
-#else
- auto __depend = nullptr;
-#endif
using _Wrapper = _Call_wrapper<_Callable, _Args...>;
// Create a call wrapper with DECAY_COPY(__f) as its target object
// and DECAY_COPY(__args)... as its bound argument entities.
_M_start_thread(_State_ptr(new _State_impl<_Wrapper>(
std::forward<_Callable>(__f), std::forward<_Args>(__args)...)),
- __depend);
+ _M_thread_deps_never_run);
}
#endif // _GLIBCXX_HAS_GTHREADS
}
void
- thread::_M_start_thread(_State_ptr state, void (*)())
+ thread::_M_start_thread(_State_ptr state, void (*depend)())
{
+ // Make sure it's not optimized out, not even with LTO.
+ asm ("" : : "rm" (depend));
+
if (!__gthread_active_p())
{
#if __cpp_exceptions
}
void
- thread::_M_start_thread(__shared_base_type __b, void (*)())
+ thread::_M_start_thread(__shared_base_type __b, void (*depend)())
{
+ // Make sure it's not optimized out, not even with LTO.
+ asm ("" : : "rm" (depend));
+
auto ptr = __b.get();
// Create a reference cycle that will be broken in the new thread.
ptr->_M_this_ptr = std::move(__b);