Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When can std::thread::join fail due to no_such_process

std::thread::join() is permitted to fail, throwing a std::system_error for no_such_process if the thread is "not valid". Note that the no_such_process case is distinct from a thread that is not joinable (for which the error code is invalid_argument).

In what circumstances might that happen? Alternatively, what must I do to ensure that join() does not fail for that reason? I want a destructor to join() some threads it manages, and of course I want the destructor to never throw exceptions. What can make a (properly constructed and not destroyed) thread "not valid".

like image 569
Raedwald Avatar asked Apr 20 '18 15:04

Raedwald


People also ask

Does join destroy a thread C++?

When thread::join() returns, the OS thread of execution has completed and the C++ thread object can be destroyed.

What will happen if you don't use the Join In thread?

The join part means the main program will wait for the thread to end before continuing. Without join, the main program will end and the thread will continue.

Does join block thread?

Join is a synchronization method that blocks the calling thread (that is, the thread that calls the method) until the thread whose Join method is called has completed. Use this method to ensure that a thread has been terminated.

How do I join threads in CPP?

To wait for a thread use the std::thread::join() function. This function makes the current thread wait until the thread identified by *this has finished executing. A C++ program is given below. It launches three thread from the main function.


2 Answers

In what circumstances might that happen?

On *nix systems, it happens when you try to join a thread whose ID is not in the thread table, meaning the thread does not exist (anymore). This might happen when a thread has already been joined and terminated, or if your thread variable's memory has been corrupted.

Alternatively, what must I do to ensure that join() does not fail for that reason?

You might test std::thread::joinable(), but it might also fail1. Just don't mess with your thread variables, and you're good to go. Simply ignore this possibility, if you encounter such an error your program better core dump and let you analyse the bug.


1) By fail, I mean report true instead of false or the other way around, not throw or crash.

like image 123
YSC Avatar answered Oct 04 '22 02:10

YSC


The no_such_process error code corresponds to a ESRCH POSIX error code. On a POSIX system std::thread::join() probably delegates to pthread_join().

  • Issue 7 of POSIX removed the possibility of an ESRCH.
  • On Linux, pthread_join may give ESRCH if no thread with the given thread ID could be found. The ID of a C++ thread is private data, so the only way the ID could be not found would be if this does not point to a properly constructed std::thread.

I conclude that this error condition can only occur as a result of an earlier action that had undefined behaviour, such as a bad reinterpret_cast or use of a dangling pointer.

like image 33
Raedwald Avatar answered Oct 04 '22 02:10

Raedwald