C++20引入了std::jthread
。据我所知,它在所有方面都比std::thread
更好。除了通常的限制,例如C++20的可用性、实现质量、与库的交互等情况外,是否存在任何场景下使用std::thread
更优?
C++20引入了std::jthread
。据我所知,它在所有方面都比std::thread
更好。除了通常的限制,例如C++20的可用性、实现质量、与库的交互等情况外,是否存在任何场景下使用std::thread
更优?
std::jthread
类似于拥有std::stop_source
的std::thread
。
C++是一种语言,其主要概念是不必为你不使用的内容付费。
因此:如果您不需要std::stop_source
,则应该优先选择std::thread
,以免为此支付。
即使您确实需要一个,出于与使用boost::intrusive_ptr
而不是std::shared_ptr
相同的原因(即能够管理数据局部性),您仍可能更喜欢不使用std::jthread
并将停止源保留在其他地方。
std::thread
也可以使用16,我的观点就无效了。 - 463035818_is_not_a_number