简体   繁体   中英

C++11 Thread is joinable but join() raises an exception

I have a strange issue with C++11 threads. Unfortunately I cannot paste the full example (given the complexity) and I cannot replicate the issue on a simpler example.

So the problem is that I have a thread which is running (nor join nor detach has been called on it). At some point another thread wants to stop this thread. The implementation simply set a boolean variable to false, and the call the join to wait for thread termination.

Well, the problem is the join. I checked that the current thread (calling the join) is different from the joined thread and joinable() returns true. Nevertheless this exception occurs:

libc++abi.dylib: terminating with uncaught exception of type std::__1::system_error: thread::join failed: No such process

This happens on macOS 10.11 but I had a colleague of mine test it on linux and it does not occur.

Any clue?

This might happen if you call fork() after creating additional threads in parent process. One important thing that differs the child process from the parent is that the child has only one thread.

So all C++ code which thinks there's a thread will be fooled and join() will throw "No such process". In this case because native call will return ESRCH.

You shouldn't create threads before calling fork().

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM