Boost logo

Boost Users :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2005-11-11 09:06:33


Bondarenko Denis wrote:
> Hello!
> I'm trying to use Boost.Thread in my application. I created my class
> with overloaded operator ( ) and pass it's object to the thread
> constructor. Thread runs, it's ok. But as I understand, when I pass my
> object to the thread constructor, thread makes a copy of it. So when I
> try to send a message (call a method) to MY object, running thread
> doesn't receive it, because it is a COPY. I solved this problem by
> giving to the thread object a pointer to a parent object (some kind of
> callback), but it's not quite suitable. How can I access the copy of
> my object, which is used by Boost.Thread? Is it possible or giving a
> pointer to parent object is the only way?

Boost.Threads doesn't expose the internal copy of the function object, but
you can use the following idiom to achieve a similar effect:

struct my_object
{
    void run() { /* do threaded things */ }
    void message() { /* presumably lock mutex and deliver a message */ }
};

int main()
{
    boost::shared_ptr<my_object> pm( new my_object );
    boost::thread thr( boost::bind( &my_object::run, pm ) );
    pm->message();
}


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