
Lars,
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
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 instance 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 crave.
-- Lars Viklund | zao@acc.umu.se _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- Chris Cleeland