Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

  1. What advantages does it offer over std::thread?
  2. Will it deprecate the existing std::thread?
like image 401
Waqar Avatar asked Jun 11 '20 13:06

Waqar


People also ask

What is a std :: Jthread?

std::jthread The class jthread represents a single thread of execution. It has the same general behavior as std::thread, except that jthread automatically rejoins on destruction, and can be cancelled/stopped in certain situations.

What is concurrency in C++?

Concurrency occurs when multiple copies of a program run simultaneously while communicating with each other. Simply put, concurrency is when two tasks are overlapped.


Video Answer


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.

like image 199
Nicol Bolas Avatar answered Oct 13 '22 05:10

Nicol Bolas