简体   繁体   English

有什么理由使用 std::thread 而不是 std::jthread?

[英]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.

相关问题 jthread vs std::async - jthread over std::async 的用例是什么 - jthread vs std::async - what is the use-case for jthread over std::async 在每次读取调用时创建 std::thread/std::jthread 的新实例 - Create new instance of std::thread/std::jthread on every read call 是否有理由在 "&&"/"||" 上使用 std::conjunction/std::disjunction 而不是折叠表达式? - Is there a reason to use std::conjunction/std::disjunction instead of a fold expression over "&&"/"||"? 你如何使用 std::jthread::get_stop_token()? - How do you use std::jthread::get_stop_token()? 是否有理由在 iterator::operator-() 上使用 std::distance() ? - Is there a reason to use std::distance() over iterator::operator-()? c++20 中的 std::jthread 是什么? - What is std::jthread in c++20? 是否有任何理由不使用异常来测试std :: map中是否存在元素 - Is there any reason to not use exceptions to test if an element exists in a std::map 在构造对象时是否有任何理由不使用std :: make_shared? - Is there any reason not to use std::make_shared when constructing objects? 如何使用std :: thread? - How to use std::thread? 如何使用 std::jthread 安全地注册 std::stop_callback - how to safely register std::stop_callback with std::jthread
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM