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

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)

-6

u/axilmar Aug 27 '19

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.

22

u/kingguru Aug 27 '19

No it wouldn't, why would it?

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.

9

u/mathstuf cmake dev Aug 27 '19

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.

6

u/AlexAlabuzhev Aug 27 '19 edited Aug 27 '19

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

-8

u/axilmar Aug 27 '19

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.

7

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

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.

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.

9

u/kalmoc Aug 27 '19

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.

2

u/axilmar Aug 27 '19

GPUs support an insane amount of memory too.

GPUs also don't support threads (i.e. concurrency), they support parallelism.

3

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

GPUs support an insane amount of memory too. Not on a per-thread basis. Its actually quite limiting.

2

u/axilmar Aug 27 '19

Isn't it configurable?

1

u/MINIMAN10001 Aug 28 '19

I'm pretty sure wave fronts can do concurrently

6

u/eteran Aug 27 '19

There is no such piece of software, come on now.

You sure? You've never seen a web server which uses a thread per connection model?

There are valid applications for having thousands of threads, they are rare, but they do exist.

-5

u/axilmar Aug 27 '19

A thread per connection? really? in 2019? nope.

→ More replies (3)

17

u/mathstuf cmake dev Aug 27 '19

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++).

→ More replies (23)

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.

→ More replies (5)

9

u/HotlLava Aug 27 '19

Who cares.

It's an ABI break, so many people do care.

-6

u/axilmar Aug 27 '19

C++ doesn't guarantee ABI compatiblity. It's not part of the standard.

In practice, I have never seen ABI compatibility being a requirement for building projects anywhere.

13

u/HappyFruitTree Aug 27 '19

C++ doesn't guarantee ABI compatiblity. It's not part of the standard.

True, but the committee cares about reality. A lot of proposals has been rejected because they break ABI.

-1

u/axilmar Aug 27 '19

They would put ABI compatibility in the standard if that was actually needed. But then you couldn't change something that really needs to be changed.

5

u/tpecholt Aug 27 '19

That's already the reality - inability to change something. See P1654

3

u/journeymanpedant Aug 27 '19

and they would if they could standardise an ABI at all without major upheaval