Boost Users :
Subject: Re: [Boost-users] Why does boost thread copy a functor?
From: Chris Cleeland (chris.cleeland_at_[hidden])
Date: 2012-01-15 20:36:10
Thanks for the explanation and suggestion! It makes sense.
In my case, the main thread is the long-lived thread, while the boost
thread is not. Moreover, there is state in the Task that is intended to be
shared between both threads.
I wonder if there's a way to make the copying more explicit? Had it not
been for the unexpected behavior of the file descriptor being closed, the
copying notion would have been even more subtle--and my misunderstanding
even harder to find.
On Sunday, January 15, 2012, Lars Viklund <zao_at_[hidden]> wrote:
> On Sun, Jan 15, 2012 at 06:25:52PM -0600, Chris Cleeland wrote:
>> The spawned thread gets an instance of Task, but it's not the same
>> as from the main.
>> What is the rationale?
> Functors in general should be expected to be copied, so it might not be
> an explicit decision.
> It is, however, a good decision. The thread of execution generally
> outlives the scope where the thread is started. If the thread used a
> reference to the functor, then pretty much _all_ cases would force the
> user to store away function objects elsewhere and destroy them only when
> the thread has terminated completely.
> It would also prevent the use of temporaries as thread callables.
> If you want to get reference semantics, use a reference_wrapper.
> boost::thread t(boost::ref(task)); will have the reference semantics you
> Lars Viklund | zao_at_[hidden]
> Boost-users mailing list
-- Chris Cleeland
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