Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why std::thread doesn't have try_join_for() and interrupt() methods

Can anybody explain me why std::thread is different from boost::thread by the following:

  1. It doesn't have try_join_for / try_join_until methods
  2. It has no interrupt method

There are some explanations from https://isocpp.org/wiki/faq/cpp11-library-concurrency:

There is no way to request a thread to terminate (i.e. request that it exit as a soon as possible and as gracefully as possible) or to force a thread to terminate (i.e. kill it). We are left with the options of

  1. designing our own cooperative “interruption mechanism” (with a piece of shared data that a caller thread can set for a called thread to check, and quickly and gracefully exit when it is set),
  2. “going native” by using thread::native_handle() to gain access to the operating system’s notion of a thread,
  3. kill the process (std::quick_exit()),
  4. kill the program (std::terminate()). This was all the committee could agree upon. In particular, representatives from POSIX were vehemently against any form of “thread cancellation” however much C++’s model of resources rely on destructors. There is no perfect solution for every system and every possible application.

But maybe there are more complete explanations? Such methods (try_join and interrupt) sometimes are very useful.

like image 774
Andrey G.A Avatar asked Apr 24 '14 09:04

Andrey G.A


1 Answers

Well, you answered your question by yourself. The important difference between boost and the c++11 standard is, that c++11 is a standard. So basically everyone had to agree with the methods and functions related to threads. But as you told us already, "Such methods (try_join and interrupt) sometimes are very useful"

So would it have been reasonable to impose that as standard to every compiler? Maybe, but if you really need it you could as well simply use the boost equivalent until it does maybe some day get into the standard.

@Howard-Hinnant suggested: There were voices on the committee adamantly claiming that cooperative thread cancellation could not be portably implemented in C++. At the time boost::thread did not have interrupt. So Anthony Williams implemented interrupt for boost largely as was currently proposed as a proof of concept that it could be portably implemented. This proof of concept was largely ignored by the committee, mainly because so much time had already been spent on the subject that we were at risk of sinking the entire standard because of this one issue.

like image 87
Theolodis Avatar answered Oct 23 '22 03:10

Theolodis