1

Topic: Thread join detach

Greetings! void thread_func (); void some_func (); int main () {std:: thread t (thread_func); some_func (); t.join ();} By the time of a call t.join () the flow t already could be completed, but nevertheless under the standard all the same it is required to cause either join () or detach () before  std:: thread - differently  std:: tread calls std:: terminate (). Explain, please, why such requirement?

2

Re: Thread join detach

Hello, Maxim Rogozhin, you wrote: > By the time of a call t.join () the flow t already could be completed, but nevertheless under the standard all the same it is required to cause either join () or detach () before  std:: thread - differently  std:: tread calls std:: terminate (). Explain, please, why such requirement? That did not breed neutral trailing , obviously. The such fuse.

3

Re: Thread join detach

Hello, Glory, you wrote: That did not breed neutral trailing , obviously. The such fuse. Why trailing if the flow was already completed?

4

Re: Thread join detach

Hello, Maxim Rogozhin, you wrote: > Why trailing if the flow was already completed? Whence to it, object referring to a flow, the nobility that it was completed? It only in  can be clarified. As far as I know, from application Terminate to already complete flow, no problems should be - if to use WinApi. In with ++ to a wrapper can be and in another way (here I do not know precisely), it is for example caused GetLastError, it produces nonzero result and because of that the exception is thrown out.

5

Re: Thread join detach

Hello, Glory, you wrote: Whence to it, object referring to a flow, the nobility that it was completed? It only in  can be clarified. As far as I know, from application Terminate to already complete flow, no problems should be - if to use WinApi. In with ++ to a wrapper can be and in another way (here I do not know precisely), it is for example caused GetLastError, it produces nonzero result and because of that the exception is thrown out. std:: terminate () causes end of all program. If business was only in trailing flows is there was simply a resource leak. It would be not mandatory the program to complete.

6

Re: Thread join detach

Hello, Maxim Rogozhin, you wrote: > Greetings! > > void thread_func (); > void some_func (); > int main () {> std:: thread t (thread_func); > some_func (); > t.join (); >} > > By the time of a call t.join () the flow t already could be completed, but nevertheless under the standard all the same it is required to cause either join () or detach () before  std:: thread - differently  std:: tread calls std:: terminate (). Explain, please, why such requirement? At first, before t.join () it is necessary to fulfill check t.joinable () differently we can take off with an exception, secondly, the standard cannot solve for the programmer, what behavior of a flow is necessary for it. join () can hang up the program, detach () easily can lead to trailing links. The call terminate () probably seemed smaller of harms But nobody hinders to write the RAII a wrapper which will cause join the automatic machine. class thread_wrapper {private: std:: : thread thread; public: thread_wrapper (std:: thread thread) {this-> thread = std:: move (thread);} ~thread_wrapper () {if (thread.joinable ()) thread.join ();}};

7

Re: Thread join detach

Hello, sergii.p, you wrote: SP> secondly, the standard cannot solve for the programmer, what behavior of a flow is necessary for it. join () can hang up the program, detach () easily can lead to trailing links. The call terminate () probably seemed smaller of harms If at the moment of a call  std:: thread the flow was not completed yet the compiler cannot decide to wait for end of a flow for the programmer or not. But if at the moment of a call  std:: thread the flow was already completed what for in this case to call std:: terminate ()?

8

Re: Thread join detach

Hello, Maxim Rogozhin, you wrote: > But if at the moment of a call  std:: thread the flow was already completed what for in this case to call std:: terminate ()? In this start the flow was completed, in the following was not in time. This time the program did not cause terminate, in following - caused. Then 10 consecutive times did not cause, and on 11 - caused. You most would like such floating behavior? It is better to install once the contract, instead of to do its dependent on a moon phase.

9

Re: Thread join detach

When the book with questions on interview ends or at you there still the pack is accumulated? As early as years on 5 suffices  on a forum?

10

Re: Thread join detach

Hello, Maxim Rogozhin, you wrote: > If at the moment of a call  std:: thread the flow was not completed yet the compiler cannot decide to wait for end of a flow for the programmer or not. > But if at the moment of a call  std:: thread the flow was already completed what for in this case to call std:: terminate ()? It seems to me, it could be implemented, but would complicate logic of operation a little. It turns out to the created flow it will be necessary to inform the bound class std:: thread on the end. It becomes for the sake of one specific case (a little strange - actually it is equivalent to a call detach, but only without explicit instructions). Probably to write to documentation that it is necessary to cause always either detach, or join much easier

11

Re: Thread join detach

Hello, Maxim Rogozhin, you wrote: > By the time of a call t.join () the flow t already could be completed, but nevertheless under the standard all the same it is required to cause either join () or detach () before  std:: thread - differently  std:: tread calls std:: terminate (). Explain, please, why such requirement? We google and read time: http://en.cppreference.com/w/cpp/thread/thread/detach we Google and it is readable two: https://tproger.ru/problems/safe-threads-in-cpp/ Following questions are better for setting on StackOverflow.com