Boost logo

Boost Users :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2007-02-03 15:56:43


hongleij_at_[hidden] wrote:
> #include <boost/bind.hpp>
> #include <boost/thread.hpp>
>
> #include <iostream>
>
>
> class BufferMoniter
> {
> public:
> void maitain()
> {
> std::cout <<" BufferMoniter" << std::endl;
> }
> };
> class LocalPeer
> {
> public:
> template <class T>
> void caller(unsigned int interval,T t)
> {
> //do something
> t();
> }
> void fork()
> {
> BufferMoniter *pBuffer=new BufferMoniter( );
> boost::thread
> thrd(caller(500,boost::bind(&BufferMoniter::maitain,pBuffer)) ; //
> that i really want.a fuctor of
> caller(500,boost::bind(&BufferMoniter::maitain,pBuffer).
>
> thre.join();
> }

The easiest way is to use boost::function<void()> to hide the type of T.
This will also take care of one additional problem with interpreting the
inner bind as a nested subexpression.

   void caller(unsigned int interval, boost::function<void()> const & t)
   {
       //do something
       t();
   }

  void fork()
  {
      shared_ptr<BufferMoniter> pBuffer( new BufferMoniter() ); // take care
of leak
      boost::function<void()> f =
boost::bind(&BufferMoniter::maitain,pBuffer);
      boost::thread thrd( boost::bind( caller, 500, f ) );

      thrd.join();
  }


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