If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Could you give an example for this, I am new in C++ thread and never use it before, the only thing bother me right now is how to make threads communicate with each other programmatically....
I am just wondering that how can a main thread know that a particular thread started in main thread has terminated and fire an event....
AND DURING this whole process that the main thread should not be blocked because this thread may be a GUI thread....
try
{
// do whatever can cause the exception
}
catch(...)
{
// signal an error
wxCommandEvent evt(....);
::wxPostEvent(m_yourFrame, evt);
// terminate the thread
}
Execeptions and threads live on "different planets" .
Exceptions are handled unwinding your execution stack up to the point where you catch them; each thread has its own stack so there is no possible communication, unless resorting to message queue and the like.
I have a similar need, but a GUI is not involved. I'm working on a robotics application so I have different motions happening in parallel. A motion in one thread can throw an exception (hitting something, timing out) and I'd like to be informed of it in the main thread that initiated the parallel motions at the point where the threads join.
For example:
void moveToXY()
{
x.startMove(); // spawns a thread
y.startMove();
x.wait(); // wait for thread (motion end)
y.wait();
}
If, say, y throws in its thread, how can I recover the error?
Nominally what's needed is a try/catch around the throwing thread that then copies the exception to the address space of the waiting thread before exit. The routine in the waiting thread (probably some variation of join()) then recovers the copy of exception and throws it to the waiting thread's catch.
The whole report is quite interesting, but you can skip down to the section titled "Threads and Exceptions" to see the part relevant to this topic.
I guess in my case I'll create a base exception class with a virtual clone() method and catch that from my main thread routine, then copy the caught exception into my base thread class to be rethrown by my join() method (which under the hood does a wxThread.Wait()).
The base thread class could also catch the classes in the stdexcept header and rethrow exceptions of those types, which should handle all STL-generated exceptions.