[英]Any reason to use std::thread over std::jthread?
So C++20 has introduced std::jthread
which, as I understand it, is just better than std::thread
in every regard.所以 C++20 引入了
std::jthread
,据我了解,它在各个方面都比std::thread
好。 So apart from the usual limitations like availabilty of C++20, quality of implementation, interaction with libraries, ... - is there any scenario where std::thread
is the better choice?因此,除了 C++20 的可用性、实现质量、与库的交互等通常的限制之外,...... - 是否有任何场景
std::thread
是更好的选择?
std::jthread
is like a std::thread
which owns a std::stop_source
. std::jthread
就像拥有std::stop_source
的 std:: std::thread
。
C++ is a language heavily built around the concept of not having to pay for what you don't use. C++ 是一种围绕不必为不用的东西付费的概念而构建的语言。
So: if you don't need a std::stop_source
, you should prefer a std::thread
so that you don't pay for one.所以:如果你不需要
std::stop_source
,你应该更喜欢std::thread
,这样你就不用付钱了。
Even if you do need one, you still might prefer not to use std::jthread
and keep your stop source elsewhere, for the same reason that you might use boost::intrusive_ptr
rather than std::shared_ptr
, ie being able to manage data locality.即使您确实需要一个,您仍然可能不喜欢使用
std::jthread
并将停止源保留在其他地方,原因与您可能使用boost::intrusive_ptr
而不是std::shared_ptr
的原因相同,即能够管理数据地方性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.