std::thread and resource deadlock
Error message
terminate called after throwing an instance of 'std::system_error' what(): Resource deadlock avoided
Explanation
Two reasons cause this error.
- The thread owner joins itself.
- Two threads join each other.
My code
I utilized the boost io_service to run a heartbeat routine in another thread. It skipped some details in the following code, but you’re still able to see a heartbeat_routine task that would bring a new one.
So, where is the problem?
Apparently, the heartbeat_routine function takes a weak pointer as arguments. It brought us to the error condition (1. The thread owner joins itself.) when it’s out of the main function which meant the service object already was released. Therefore, the heartbeat_routine function would hold the last pointer. As the function ended and the service object needed to be destructed, the system tried to destroy the thread and caused the error.
If this post is helpful, please give a thumb. Thanks!
If you like this post, you can buy me a coffee! :)