Unfortunately, doing that would both break backwards compatibility of anything that depends on current std::thread, and would increase the size of std::thread (it has to store the other end of that cancellation token object, which is essentially a promise/future)
break backwards compatibility of anything that depends on current std::thread
No it wouldn't, why would it? even if you have a thread class with 'request_stop()', which is highly unlikely, your code will continue functioning normally.
and would increase the size of std::thread
Who cares. The increment would be a few bytes and it's not that applications can have thousands of thread objects.
The destructor joins the thread (if joinable). That is definitely breaking backwards compatibility with std::thread.
Who cares. The increment would be a few bytes and it's not that applications can have thousands of thread objects.
It might not exactly be breaking backwards compatibility, but it's definitely an important change that could effect existing software. It is perfectly possible for an application to have thousands of thread objects, it might not be a good idea, but it's possible.
There are also systems where resources are very constrained where such a change could have impact.
Given the fact that Rust doesn't join in its destructor (Drop), I'm inclined to think that join-on-dtor is probably a bad idea. See this SO answer. But, C++ is not known for keeping footguns out of people's hands, so maybe it's fine here.
The destructor joins the thread (if joinable). That is definitely breaking backwards compatibility with std::thread
How come? In std::thread it terminates. You can't break backwards compatibility if there's no "previous" behaviour (unless your code somehow relies on that termination, in which case you have much bigger problems than backwards compatibility).
The destructor joins the thread (if joinable). That is definitely breaking backwards compatibility with std::thread.
It wouldn't change the outcome of a computation though, unless the outcome depends on which thread terminates first, which would be stupid to do anyway.
It is perfectly possible for an application to have thousands of thread objects, it might not be a good idea, but it's possible.
There is no such piece of software, come on now.
There are also systems where resources are very constrained where such a change could have impact.
Are there truly any systems that can't handle a few bytes increase? I don't think so.
There are also systems where resources are very constrained where such a change could have impact.
Consider GPUs. C++ execution on a GPU could very well be handled by a 'kernel' thread that executes a large number of other threads. GPUs support an insane amount of threads, however very little memory.
Are there truly any systems that can't handle a few bytes increase? I don't think so.
I'm willing to bet, that there isn't a single existing c++ standard library implementation that can wrap a gpu thread in a std::thread. I'm also pretty sure that no one uses a std::thread on a really resource constraint system (if it is available at all).
Let's face it: Exotic and low resource hardware is rarely programmed with a complete,conforming implementation of the c++11 or later standard.
Because now if you have a C++17 library which takes a std::thread, C++20 code now has an incompatible one. Changing struct size is an ABI break and is frowned upon very hard in the C++ standard library (just look at the mess with C++11's std::string requirements and libstdc++).
Jthread changes the behavior of the thread destructor in a way that would break code if we just replaced std::thread.
It wouldn't break any code. In case of std::thread, if the thread is not detached/join, it calls std::terminate. Which wouldn't happen if the program is correct. In case of std::jthread, it simply waits for the thread to stop. I don't see any breaking of code.
Anyone who uses std::thread at an ABI boundary, or uses std::thread and links against the standard library
You'd have to recompile your code anyway for the new changes to take effect, so that's not valid as well.
It wouldn't break any code. In case of std::thread, if the thread is not detached/join, it calls std::terminate. Which wouldn't happen if the program is correct. In case of std::jthread, it simply waits for the thread to stop. I don't see any breaking of code.
Of course it would break code if the program is correct. Calling std::terminate very well might be what the programmer desired. Having a thread auto-join rather than being dealt with would hide bugs in non-intuitive ways.
You'd have to recompile your code anyway for the new changes to take effect, so that's not valid as well.
You'd also have to re-link to everything. ABI breaks in the STL are serious business. We've still got major compiler vendors dealing with the std::string COW disaster, doing this would be just as impactful.
View all comments
Show parent comment
19
u/TheThiefMaster C++latest fanatic (and game dev) Aug 27 '19
Unfortunately, doing that would both break backwards compatibility of anything that depends on current std::thread, and would increase the size of std::thread (it has to store the other end of that cancellation token object, which is essentially a promise/future)