views:

631

answers:

1

Can someone please show a simple, but complete example of how one could use Boost exception library to transfer exceptions between thread by modifying the code below?

What I'm implementing is a simple multi-threaded Delegate pattern.

class DelegeeThread
{
public:
  void operator()()
  {
     while(true)
     {
       // Do some work

       if( error )
       {
         // This exception must be caught by DelegatorThread
         throw std::exception("An error happened!");
       }
     }
  }
};

class DelegatorThread
{
public:
  DelegatorThread() : delegeeThread(DelegeeThread()){}  // launches DelegeeThread
  void operator()()
  {
    while( true )
    {
       // Do some work and wait

       // ? What do I put in here to catch the exception thrown by DelegeeThread ?
    }
  }
private:
  tbb::tbb_thread    delegeeThread;
};
+2  A: 

You might want to use Boost::Exception to solve this problem. Here is an example of how to use their exception lib to get the exception in the calling thread: http://www.boost.org/doc/libs/1%5F40%5F0/libs/exception/doc/tutorial%5Fexception%5Fptr.html

If I remember well, C++0x will provide a mechanism to allow something similar to solve this particular problem.

Klaim
yes, i agree we need to use Boost::Exception. I have already looked at an example, but I didnt really get how to use it.
ShaChris23