[英]Why does std::thread wait in its constructor?
The current implementation of the STL in Microsoft Visual C++ 2015 seems to disallow any lock-free environment to start a thread due to a mutex wait in the constructor. 由于构造函数中的互斥锁等待,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)...));
}
Can anybody tell me why this wait is needed? 谁能告诉我为什么需要等待吗?
I'm asking, because I currently examine a system that sometimes needs over 500ms to construct a std::thread
but never shows this delay with using CreateThread
. 我问,是因为我目前正在检查一个有时需要500毫秒才能构造
std::thread
的系统,但从未使用CreateThread
表现出这种延迟。
_LaunchPad<_Target> _Launcher(_STD forward<_Target>(_Tg));
_Launcher
is on the stack and is passed to the new thread. _Launcher
在堆栈上,并传递给新线程。 Why? 为什么? I don't know, but this makes it important that
_Launcher
not go out of scope until the new thread is done with it. 我不知道,但这使得
_Launcher
在使用新线程完成之前不要超出范围很重要。 The wait is likely there to guarantee this. 等待可能会保证这一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.