Boost logo

Boost Users :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2004-08-27 09:56:56


Doug Gregor wrote:
> On Aug 27, 2004, at 4:37 AM, Howard Cole wrote:
>>
>> myclass test_instance;
>> boost::thread test_thread(test_instance);
>> test_thread.join();
>
> Just change the thread construction to use boost::ref:
>
> boost::thread test_thread(boost::ref(test_instance));

Yep. In addition you might want to consider using shared_ptr, because your
ref-enhanced code will now need to keep track of test_instance and ensure
that it isn't destroyed before the thread has finished.

    shared_ptr<myclass> px( new myclass );
    thread test_thread( &myclass::do_thread_stuff, px );

You can also use pimpl to hide the shared_ptr in myclass, but the above is
easier, and you can make myclass noncopyable.


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