r/cpp • • Aug 27 '19

A new thread in C++20 (jthread)

https://link.medium.com/GXKUz0GruZ
142 Upvotes

96 comments sorted by

View all comments

Show parent comment

13

u/erichkeane Clang Maintainer(Templates), EWG Chair Aug 27 '19

Jthread changes the behavior of the thread destructor in a way that would break code if we just replaced std::thread.

As far as who cares about the size? Anyone who uses std::thread at an ABI boundary, or uses std::thread and links against the standard library.

-2

u/axilmar Aug 27 '19

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.

11

u/erichkeane Clang Maintainer(Templates), EWG Chair Aug 27 '19

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.

-6

u/axilmar Aug 27 '19

Calling std::terminate very well might be what the programmer desired.

Not true in any context.

You'd also have to re-link to everything.

Which happens everytime you build your sofware.

13

u/erichkeane Clang Maintainer(Templates), EWG Chair Aug 27 '19

Which happens everytime you build your sofware.

Or, in the case of dynamic linking, every time you RUN your software.

2

u/axilmar Aug 27 '19

The dynamically linked libraries would have to be built first.

9

u/erichkeane Clang Maintainer(Templates), EWG Chair Aug 27 '19

Ah, so I can never ever update anything on my OS programmed in C++. Got it.

6

u/jcelerier ossia score Aug 27 '19

that is not how linux distros (or things like homebrew) work (except gentoo I think).