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 21:01:40


On Sun, Jan 15, 2012 at 07:36:10PM -0600, Chris Cleeland wrote:
> 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.

Please do not top post, the list guidelines encourage bottom posting.

Your wrapper does not seem to follow the rule of three:
If a class defines one of the following it should probably explicitly
define all three:
* destructor
* copy constructor
* assignment operator

Your wrapper type does not seem to have provided adequate
implementations for the cctor and op=, or has not prevented the
synthesised ones from existing.

If you intend for a type to be noncopyable, either inherit from
boost::noncopyable or make the copy constructor and assignment operator
private.

That way, if someone accidentally copies your type, you will get a nice
compile-time error.

Another alternative is that you can hold your state in the Task by
reference or smart pointer, such that if it's copied, the guts still
refer to the correct state.

-- 
Lars Viklund | zao_at_[hidden]

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