Exceptions across threads

Hi, I have a class that starts and stops a thread. If an exception occurs inside the thread, the function will silently stop without signaling the main about the error.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
class Driver{
  public:
    Driver(){}
    ~Driver(){}
    void start()
    {
       m_terminate = false;
       myThread = boost::thread(loop, this);
    }
    void stop()
    {
      m_terminate = true;
      myThread.join();
    }
  private:
    bool m_terminate;
    boost::thread myThread ;
}

void loop()
{
  try
  {
    while(!m_terminate)
    {
      //Do something..
      throw 1;  //something went wrong, throw an exception.
    }
  }
  catch(int)
  {
    throw; // Rethrow. This will not be caught by the main()!!
  }
}

int main()
{
  Driver d;
  try
  {
    d.open()
    // Do something..
    // ..
    d.close()
  }
  catch(int)
  {
    //how to catch here an exception occurred in loop()?
  }
  return 0;
}


Is there a way to propagate the exception up to the main?
I don't want to add a new method to the Driver to poll the thread's state.

Thanks for your help.
Exceptions do not hop threads, they occur within a thread of execution.
There isn't. You should catch all exceptions in the thread's main function and have the thread
main return an error code to the parent.
Topic archived. No new replies allowed.