Boost logo

Threads-Devel :

Subject: Re: [Threads-devel] Threads and copy constructors...
From: Anthony Williams (anthony_at_[hidden])
Date: 2012-02-21 04:38:37


On 18/02/12 19:37, oneflewover wrote:
> In the code snippet below, I have one functor that I pass to one thread.
> As expected, the constructor is called once, but the copy constructor is
> called four times! As a result, the destructor is called five times.
>
> Can someone please explain to me why the copy constructor is called four
> times? Thanks.

Which compiler are you using? If your compiler doesn't support rvalue
references then there are lots of copies because various functions take
things by value.

The thread constructor: thread(F)
which calls: make_thread_info(F)
which constructs a thread_data<F>: thread_data(F)
which constructs the thread_data<F> member.

=> 4 copies. With optimization enabled, some of these can be optimized out.

Anthony

-- 
Author of C++ Concurrency in Action     http://www.stdthread.co.uk/book/
just::thread C++11 thread library             http://www.stdthread.co.uk
Just Software Solutions Ltd       http://www.justsoftwaresolutions.co.uk
15 Carrallack Mews, St Just, Cornwall, TR19 7UL, UK. Company No. 5478976

Threads-Devel list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk