Boost logo

Boost Users :

Subject: Re: [Boost-users] Why does boost thread copy a functor?
From: Lars Viklund (zao_at_[hidden])
Date: 2012-01-15 19:49:31

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

Lars Viklund | zao_at_[hidden]

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at