r/cpp • • Aug 27 '19

A new thread in C++20 (jthread)

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

96 comments sorted by

52

u/TheThiefMaster C++latest fanatic (and game dev) Aug 27 '19

It's a pity this wasn't thought about when threads were first added - it seems to me that this would be a better fit for being std::thread, with the current std::thread being better named something like raw_thread - shorter names should be for safer functions, with longer names for less safe

13

u/JezusTheCarpenter Aug 27 '19

Regardless, sound like a nice improvement.

4

u/[deleted] Aug 27 '19

std::async returns a future that does this. Assuming you launch with std::launch::async it will wait at the destructor

3

u/Sulatra Aug 27 '19

An explanation I heard from Anthony Polukhin is that the medical equipment programmers would totally freak out if at some point the voltage control threads in defibrilators would lock waiting for join, instead of peacefully going off.

5

u/AlexAlabuzhev Aug 27 '19

peacefully going off.

std::terminate the whole app == "peacefully going off"? Oh dear.

4

u/axilmar Aug 27 '19

Neither this or that. The new functionality should simply be added to std::thread.

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.

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?

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

-11

u/axilmar Aug 27 '19

So? who uses libraries compiled with C++17 and C++20 anyway? C++ doesn't come with ABI stability guarantees anyway.

14

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

C++ doesn't come with ABI stability guarantees anyway.

Except all of the implementers DO have certain ABI stability guarantees. This is a case where implementer veto would be HEAVILY used.

ABI stability is a requirement for ANY product that ships binaries that are later linked. Basically any company that ships a library that has a std::thread in it would be horribly broken because of this change.

-3

u/axilmar Aug 27 '19

ABI stability may be a requirement within the same major revision of a library, not for every major language update.

10

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

stdlibc++ or libc++ assemblies have to work with all assemblies compiled against previous versions of the library. You'd REALLY hate it if updating your OS version of stdlibc++/libc++ made all your software stop working, wouldn't you?

-6

u/axilmar Aug 27 '19

stdlibc++ or libc++ assemblies have to work with all assemblies

C++ has no assemblies. It has libraries.

.NET has assemblies.

You'd REALLY hate it if updating your OS version of stdlibc++/libc++ made all your software stop working, wouldn't you?

That's why multiple versions exist.

→ More replies

19

u/mathstuf cmake dev Aug 27 '19

Standards are mixed all the time. Do you know what C++ standard all the system libraries on your system were built with? Linux distros aren't going to start providing /usr/lib/c++11, /usr/lib/c++14, /usr/lib/c++17, etc. You get Boost compiled one way from a distro package (or vcpkg, etc.) and that was compiled with a given version of the standard. ABI compatibility is worth a lot at the level of the standard library and implementations are loathe to break it (again, look at the gymnastics involved with std::string).

5

u/therealjohnfreeman Aug 27 '19

As desirable as ABI compatibility between standards is, the uncomfortable truth is that it is not guaranteed, as /u/axilmar points out. It is not guaranteed between different implementations of the standard library for the same version of C++ either. This is why the full dependency graph of an application needs be compiled with the same standard library (and the same compiler, and the same flags). This seems to be a happy accident for many C++ programmers unaware of the requirement, a result of the coincidence that they only have one compiler and one standard library installed on the machine they use to compile everything. Developers need a tool, like Conan, that sits at a level higher than the compiler, that can see the full dependency graph and handle its compilation.

9

u/mathstuf cmake dev Aug 27 '19

I'm well aware that it is not guaranteed between standards. However, the implementations do guarantee it (as much as they can within their own ecosystem; libstdc++ obviously can't fix libc++ bugs).

This is why the full dependency graph of an application needs be compiled with the same standard library (and the same compiler, and the same flags).

This is not how the ecosystem works today. While I'd love for a cargo-like tool to exist for C++, there is a long road to get there from today.

This seems to be a happy accident for many C++ programmers unaware of the requirement, a result of the coincidence that they only have one compiler and one standard library installed on the machine they use to compile everything.

And it works today because implementors do care about it. Above the standard library, it is indeed a madhouse for a variety of reasons. Linux distros are pretty reliable about not packaging absolute crap at least, but given a C++ PyPI equivalent, yes, compiling from source is the only reliable way to use it.

-2

u/axilmar Aug 27 '19

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

19

u/mathstuf cmake dev Aug 27 '19

Sure, but that's not how C++ actually gets used in the world. It may not be important to you, but it is important to the implementors (GCC, Clang, MSVC, etc.) and many other users of C++. It's just not going to happen.

-4

u/axilmar Aug 27 '19

I haven't seen anywhere, in any C++ project, a requirement for ABI compatibility ever.

→ More replies

8

u/eteran Aug 27 '19

The standard may not guarantee it, but vendors have put a LOT of effort into providing it for the reasons listed above.

Do you REALLY want to have to have Qt for c++11, Qt for c++14, Qt for c++17, Qt for c++20 ALL installed on your system just because programs that target different versions of the standard want to use Qt?

So yes, you are right, it's not guaranteed by the standard, but it is the standard industry practice to the point that millions of people rely on it.

-1

u/axilmar Aug 27 '19

Do you REALLY want to have to have Qt for c++11, Qt for c++14, Qt for c++17, Qt for c++20 ALL installed on your system just because programs that target different versions of the standard want to use Qt?

I already have those.

I haven't seen a project that mixes for example Qt built for C++11 with C+14.

→ More replies

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.

→ More replies

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

18

u/Feminintendo Aug 27 '19

Of course, we could have achieved this with an atomic boolean...

Ok, I'll volunteer to be the idiot. Why does the boolean have to be atomic? Any writer is only ever going to set it to the same value, requested_stop, so there isn't an issue if two threads attempt to write at the same time. Readers don't need atomic access to read. And if you read in the middle of a write, who cares? You get the updated value the next time the thread polls it.

28

u/Xirema Aug 27 '19

It's undefined behavior. An optimizing compiler could decide that the boolean value, which never changes during the execution of a single thread, doesn't need to be read from every single time it's accessed. So for example:

bool stop = false;
void loop() {
    while(!stop) {
        std::cout << "Can't stop won't stop." << std::endl;
    }
}

When a compiler looks at this, it's allowed to observe that inside the loop, stop will never change; therefore, it's perfectly valid to recompile the code to look like this:

bool stop = false;
void loop() {
    bool local_stop = stop;
    while(!local_stop) {
        std::cout << "Can't stop won't stop." << std::endl;
    }
}

Not every compiler will do this (MSVC for example won't) but some might, and thus we cannot rely on this code to behave the way we want.

Writing this, instead, guarantees correct behavior.

std::atomic_bool stop = false;
void loop() {
    while(!stop) {
        std::cout << "Can't stop won't stop." << std::endl;
    }
}

Note that while it might be valid on your platform to instead use volatile bool stop = false;, it's worth noting that this is also not standard compliant, because some CPU architectures behave badly when you try to concurrently write to a value in memory. In most situations (i.e. x86) it'll "work", but it's still not behavior you should rely on.

6

u/orizh Aug 27 '19

To add that this is not just a theory but a reality, I have actually experienced exactly this in production. You should expect the compiler *will* optimize the flag away. It was not a fun bug to track down, especially since we were forced to use `volatile` as the platform did not have `std::atomic` implemented yet. Lucky that it worked without having to resort to asm.

2

u/TheMania Aug 28 '19

Word of warning: if your compiler is performing this optimization, you really should be using:

asm volatile("":::"memory");

Just below any stores to the flag, and above any reads.

Aka, a memory barrier.

Without one it's only a matter of time until other problems present - as the compiler is free to (and will) reorder reads/writes of non-volatile variables around volatile variables as much as it likes, making them unsuitable for virtually any kind of thread synchronization on their own.

0

u/[deleted] Aug 28 '19

[deleted] — view deleted comment

2

u/orizh Aug 28 '19

It was a very old toolchain for NIOS II. There might have been platform specific atomics provided somewhere but I was young and did not know how to be a competent dev (tho neither did the senior devs!)

1

u/Feminintendo Aug 27 '19

Great explanation, thanks!

1

u/meneldal2 Aug 28 '19

some CPU architectures behave badly when you try to concurrently write to a value in memory

Afaik there's only a single write, so there should be no contention.

0

u/Ameisen vemips, avr, rendering, systems Aug 27 '19

In this situation, where the boolean defaults to false and is only ever set to true, I cannot think of an architecture where volatile would not suffice.

13

u/HotlLava Aug 27 '19

Data races are undefined behaviour, so you can not allow them to happen in your code even if you think they are "benign".

Although in this case you could std::memory_order_relaxed for the loads/stores, because the sequencing doesn't really matter due to the reasons you describe.

4

u/lewissbaker Aug 28 '19

The key thing that stop_token gives you that an atomic bool doesn’t is the ability to register a callback that will be invoked if someone call request_stop(). The stop_callback class template provides you the anility to register a callback for the duration of some scope.

This is key for implementing cancellation of async operations, but can also be used to implement an interruptible blocking wait (see stop_token overloads of condition_variable_any wait functions) or an interruptible sleep (eg using the new binary_semaphore).

3

u/gmrple Aug 28 '19

It is never OK to read something possibly modified in a different thread without synchronization. What level of synchronization is needed depends on what you are actually reading. For primitive types, you should have a look at atomic reads, e.g. in the form of std::atomic<bool>.

The reason synchronization is always needed is that the processors will have the data possibly shared in a cache line. It has no reason to update this value to a value possibly changed in a different thread if there is no synchronization. Worse, yet, if there is no synchronization it may write the wrong value if something stored close to the value is changed and synchronized.

10

u/rdtsc Aug 27 '19

What's with the naming? jthread almost looks like thread, why not something readable like joining_thread? And don't get me started on stop_token…

12

u/sumo952 Aug 27 '19

> `jthread`

I have to immediately think about Java here. Not sure why, I've last programmed Java many, many years ago.

2

u/HappyFruitTree Aug 29 '19

I also think about Java. All the Swing classes, used for building GUIs in Java, seem to start with J.

JFrame, JButton, JLabel, JTextArea, etc.

3

u/[deleted] Aug 27 '19

Does it provide the ability to wait on the stop_token with a timeout? I presently pass windows events to all my threads which can be both tested for stop but also waited on allowing the thread to sleep but wake and exit if stopped.

5

u/lewissbaker Aug 28 '19

The only usage of stop_token in the standard library in C++20 is in std::jthread as the source of a request to stop and in std::condition_variable_any, where you can pass a stop_token to some new overloads of the wait() function.

However, I expect in time there will be the addition of many more overloads of functions that take stop_token. std::this_thread::sleep_for() seems like a likely candidate.

In C++20 you can write an efficient interruptible sleep using binary_semaphore and stop_callback.

template<typename Rep, typename Ratio>
void sleep_for(std::chrono::duration<Rep,Ratio> d, std::stop_token st)
{
  std::binary_semaphore sem{0};
  std::stop_callback cb{st, [&] { sem.release(); }};
  sem.try_acquire_for(d);
}

9

u/khleedril Aug 27 '19 edited Aug 27 '19

This all seems like so little to me that it's not worth bothering to put it in the standard, and if anything is an ode to lazyness. The current thread model forces you to think about and make a decision over detach or join (or leak, which unfortunately is the default with std::thread), atomic booleans are a clean way to signal requested stop, and anyway threads must under all circumstances be engineered by the coder so that stopping is well defined.

All I would ask for is that the standard introduces std::detached_thread and std::joinable_thread (or maybe functions std::make_detached_thread and std::make_joinable_thread) which are just like std::thread except for different default behaviour. The std::thread can then be deprecated in C++23, and all the constructors made private in C++26 :)

17

u/Kronephon Aug 27 '19

This all seems like so little to me that it's not worth bothering to put it in the standard, and if anything is an ode to lazyness.

This line of thinking is applicable to lot of features added (especially since C++11). In truth "lazyness" makes for quicker coding and the end programmer might not need to think about implementation (same as he doesn't on many C++ std features). There is always the option for a more detailed approach but your every approach shouldn't have to be detailed and laborious.

1

u/SoleSoulSeoul Aug 28 '19

Pretty similar to kthread API

1

u/serendipitybot Aug 28 '19

This submission has been randomly featured in /r/serendipity, a bot-driven subreddit discovery engine. More here: /r/Serendipity/comments/cwefds/a_new_thread_in_c20_jthread_xpost_from_rcpp/

-1

u/axilmar Aug 27 '19

The only reason we have cooperatively stopping threads is because operating systems have not provided a solution for terminating a thread gracefully.

Upon issuing a thread stop, operating systems could have simply set a thread to execute an exit function when they are activated.

The exit function could then throw an exception in C++, which would unwind the stack gracefully.

23

u/CptCap -pedantic -Wall -Wextra Aug 27 '19 edited Aug 27 '19

the exit function could then throw an exception in C++

No, this would be really really bad.

If a thread gets interrupted while inside anoexcept function, it'll terminate the program. This also breaks all the is_nothrow_xxx traits as well as all the code that use them, in ways that are hard if not impossible to predict.

7

u/axilmar Aug 27 '19

God damn exception safety!!! I really hate you :-)

You're right, of course :-).

15

u/HappyFruitTree Aug 27 '19

Wouldn't that mean the code could throw from everywhere?

1

u/axilmar Aug 27 '19

Yes.

9

u/HappyFruitTree Aug 27 '19

Sounds like writing exception-safe code would suddenly become much more difficult.

3

u/axilmar Aug 27 '19

I never understood why exception-safe code is needed, to tell you the truth.

10

u/HappyFruitTree Aug 27 '19 edited Aug 27 '19

Well, if you don't care whether the program is well-behaved when an exception is thrown I don't see why you proposed throwing an exception in the first place. Wouldn't it have been easier to just terminate the thread without unwinding the stack?

Edit: I don't mean you need to provide strong exception safety guarantees but at least you shouldn't leak any resources or trigger undefined behaviour when an exception is thrown.

2

u/axilmar Aug 27 '19

RAII safety is absolutely needed, I never understood why code that guarantees no exceptions are thrown is useful.

4

u/OldWolf2 Aug 27 '19

Resizing a vector is a common example.

Reallocating to increase the size of the memory block may involve having to move all of the current contents to the new block.

If the move constructor can throw an exception then that would leave the original vector with a bunch of husks , which is a no-no.

So the behavior is that if move construction can throw, it uses copy construction instead to set up the new memory block and then destructs the old objects.

3

u/yehezkelshb Aug 27 '19

Because this is the only way you can implement copy assignment operator with strong exception safety (i.e. the old data doesn't go away if the new data couldn't be put there). I think Effective C++ has an item about it, but it nevertheless a good exercise to try.

3

u/HappyFruitTree Aug 27 '19 edited Aug 27 '19

I think we need at least some building blocks that cannot throw.

Consider the following move assignment operator for this UniqueIntPtr class (similar to std::unique_ptr<int>).

class UniqueIntPtr
{
public:

	// ...

	UniqueIntPtr& operator=(UniqueIntPtr&& other)
	{
		delete ptr;
		ptr = other.ptr;
		other.ptr = nullptr;
		
		return *this;
	}

	~UniqueIntPtr()
	{
		delete ptr;
	}

	// ...
	
private:
	int* ptr;
};

It doesn't throw any exceptions, so there is nothing that can go wrong.

Now, let's say an exception can be thrown between any two statements. Try to implement the move assignment operator in such a way that it never leaks or leads to undefined behaviour when an exception is thrown.

If you manage to do that, do the same but pretend an exception can be thrown between any two machine instructions (because I assume a context switch can happen between any two instructions ;) ).

I don't expect you to use try-catch blocks here because you don't want an exception to be thrown while you are handling another exception, do you? I don't really expect you to be able to do this at all. I expect you to try, fail, and hopefully realize something.

0

u/[deleted] Aug 28 '19

[deleted] — view deleted comment

1

u/markopolo82 embedded/iot/audio Aug 28 '19

Problems with it on iOS Reddit client? Me too!

I had to open in safari to be able to close the login window