Reputation: 7118
I have multithreaded application using C++ (not C++11) using pthread. SO, I have couple of threads running in parallel with corresponding thread functions. In main thread block, I have try-catch block but within thread function, I don't. Each thread function (other than main) runs while(1) loop and breaks when a certain condition is set by main thread indicating to exit. The condition variable check and setting are all done within mutex guards to ensure mutual exclusion. However, within a thread function, some exception occurred and the thread became a zombie and the application crashed. pstack core could not show the stack trace properly, as that might have been corrupted. My question is: should I use try-catch block to handle exception with thread function too? Of course outside the try block should have while(1) block within and catch block should handle the exception gracefully and then probably gracefully end. Can I pass an exception from a child thread passed to the second thread? Ideally not. What should be the best practice?
Upvotes: 1
Views: 323
Reputation: 24857
should I use try-catch block to handle exception with thread function too?
Yes, because exceptions are a stack-based mechanism. Since each thread has its own stack, it has its own exceptions.
If you have no language support for communicating exceptions, you will have to resort to 'manually' copying the exception obect in the catch and signaling to whatever thread needs to know about it with some inter-thread comms that are specific to your design.
Upvotes: 2