1

I'm reading the "C++20 for Programmers An Objects-Natural Approach, 3rd Edition" and came across a paragraph in the book that discusses several features of std::jthread, specifically:

"Class jthread also fixes other problems with thread. In particular, jthread supports cooperative cancellation (Section 17.9), supports proper move semantics, and is an RAII type (discussed in Section 11.5) that correctly cleans up the resources it uses."

I'm trying to understand how std::jthread supports "proper move semantics."
Both std::thread and std::jthread support move semantics, but if the author's point about std::thread not joining after thread objects leave scope relates to RAII compliance, then it seems to fall into the category of not being RAII-compliant! Could someone clarify what is meant by "proper move semantics" in this context? How does std::jthread handle move semantics differently or more effectively compared to std::thread?

2
  • only the author can answer that, but std::thread supports move semantics, specifically joinable() will return false in the moved-from state, it follows the rule of 5 correctly, if the std::thread is joinable() and its destructor was called without joining or detaching then std::terminate is called, it is a fully specified flawless moveable object. Commented Oct 10, 2024 at 19:01
  • to be honest i think calling std::terminate is a surprise for any python developer when they expect the destructor to join the thread as done in some languages ... but there's C which doesn't care what happens to the thread and there's rust that simply detaches the thread if it wasn't joined ... why should std::thread follow python way and not C or rust's way ? Commented Oct 10, 2024 at 19:17

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.