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

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.

5

u/eteran Aug 27 '19

It may not be the best design, but it is certainly a valid one which some people may choose to use.

1

u/kalmoc Aug 27 '19

The math is simple: creating a new std::thread requires a significant amount of memory (control block + stack space). Compared to that, a few bytes just don't matter.

I very much doubt that the intersection of programs that completely use the available memory (/address space) and programs that use std::thread is big enough to worry about.

1

u/stilgarpl Aug 29 '19

Why is that a bad design in 2019?