Boost logo

Boost Users :

From: bill_kempf (williamkempf_at_[hidden])
Date: 2002-09-05 17:36:19


--- In Boost-Users_at_y..., Luis De la Parra <lparrab_at_g...> wrote:
>
> hi all,
> is there a way to get a reference to the function object used by a
> boost::thread object??

No, and it would be very difficult to do so, since the type is lost.

> when you create a new thread and feed it a function object it
copies it and
> then starts a new thread of execution on this object.. well, I
would like to
> send messages to the object in which the thread is running but I
don't know
> how to do it with boos.threads...

Only by wrapping it in an adapter. The boost::ref class is a very
good candidate for this. Here's an example, and I'll add this to
the next release:

#include <boost/ref.hpp>
#include <boost/thread/thread.hpp>
#include <iostream>

struct thread_func
{
   thread_func() : val(0) { }
   void operator()() { val = 99; }
   int val;
};

int main()
{
   thread_func f;
   boost::thread thrd(boost::ref(f));
   thrd.join();
   std::cout << f.val << std::endl; // should be "99"
}

Bill Kempf


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