Boost logo

Boost Users :

From: Christian Henning (chhenning_at_[hidden])
Date: 2005-08-11 13:00:43


On 8/11/05, Domenico Andreoli <cavok_at_[hidden]> wrote:
> On Thu, Aug 11, 2005 at 01:24:08PM -0400, Christian Henning wrote:
> > Thank you very much for the useful information. I think I have to
> > learn a lot more about the Posix threads.
>
> i usually make a join on the thread i want to wait for it to exit. of
> course my threads are joinable :)
>
That is being done in the fActive class destructor. The fActive is RAII class.

Here is the fActive class:

template<class _T_>
class fActive : private boost::noncopyable
{
public:
   typedef void (_T_::*tfpActiveMethod)() throw();

public:
   fActive()
         throw();

  ~fActive()
         throw();

   //
   // Thread creation
   //

         void
   create( _T_ * pActiveObject,
           tfpActiveMethod pActiveMethod )
         throw( fInvalidParameterException,
                fStandardException );
         
private:
   // we need a pointer since we contruct the thread inside the create()
   // and there is no assignment allowed for thread objects
   boost::thread* _pThread;

   _T_ * _pActiveObject;
   tfpActiveMethod _pActiveMethod;

   bool _bCreated;

};

//-----------------------------------------------------------------------------
// fActive inline methods
//

      template<class _T_>
fActive<_T_>::fActive()
      F_CE( throw() )

   : _pThread ( NULL )
   , _pActiveObject( NULL )
   , _pActiveMethod( NULL )

   , _bCreated( false )
{
   ;
}
      template<class _T_>
fActive<_T_>::~fActive()
       throw()
{
   if( _bCreated )
   {
      _pThread->join();

      delete _pThread;
      _pThread = NULL;
   }
}

      template<class _T_>
      void
fActive<_T_>::create( _T_ * pActiveObject
                    , tfpActiveMethod pActiveMethod )
      throw( fInvalidParameterException,
             fStandardException )
{
   F_ASSERT_PARAM( pActiveObject != NULL );
   F_ASSERT_PARAM( pActiveMethod != NULL );

   _pActiveObject = pActiveObject;
   _pActiveMethod = pActiveMethod;

   try
   {
      _pThread = new boost::thread( boost::bind( _pActiveMethod
                                               , _pActiveObject ));
   }
   catch( std::exception ex )
   {
      fStandardException oNewException( ex,
                                          "Creating thread in active object" );

      F_THROW( oNewException );
   }

   _bCreated = true;
}

Greets,
Christian


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net