Microsoft Visual C++ 2015 中 STL 的当前实现似乎不允许任何无锁环境启动线程,因为构造函数中存在互斥等待。
void _Launch(_Thrd_t *_Thr)
{ // launch a thread
_Thrd_startX(_Thr, _Call_func, this);
while (!_Started)
_Cnd_waitX(_Cond, _Mtx); // <-- Why?
}
template<class _Target> inline
void _Launch(_Thrd_t *_Thr, _Target&& _Tg)
{ // launch a new thread
_LaunchPad<_Target> _Launcher(_STD forward<_Target>(_Tg));
_Launcher._Launch(_Thr);
}
explicit thread(_Fn&& _Fx, _Args&&... _Ax)
{ // construct with _Fx(_Ax...)
_Launch(&_Thr,
_STD make_unique<tuple<decay_t<_Fn>, decay_t<_Args>...> >(
_STD forward<_Fn>(_Fx), _STD forward<_Args>(_Ax)...));
}
谁能告诉我为什么需要等待?
我在问,因为我目前正在检查一个系统,该系统有时需要超过 500 毫秒来构建 astd::thread
但从不显示使用CreateThread
.