Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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?

like image 846
flowit Avatar asked Mar 29 '26 20:03

flowit


1 Answers

std::jthread is like a std::thread which owns a std::stop_source.

C++ is a language heavily built around the concept of not having to pay for what you don't use.

So: if you don't need a std::stop_source, you should prefer a std::thread so that you don't pay for one.

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, i.e. being able to manage data locality.

like image 195
Tommy Avatar answered Apr 01 '26 14:04

Tommy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!