What is std::jthread in c++20?

Viewed 4705
  1. What advantages does it offer over std::thread?
  2. Will it deprecate the existing std::thread?
1 Answers

std::jthread is like std::thread, only without the stupid. See, std::thread's destructor would terminate the program if you didn't join or detach it manually beforehand. This led to tons of bugs, as people would expect it to join on destruction.

jthread fixes this; it joins on destruction by default (hence the name: "joining thread"). It also supports a mechanism to ask a thread to halt execution, though there is no enforcement of this (aka: you can't make another thread stop executing).

At present, there is no plan to deprecate std::thread.

Related