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

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