Boost C++ Libraries: Ticket #2144: boost::thread constructor wants to make copies of the argument https://svn.boost.org/trac10/ticket/2144 <p> Hi, </p> <p> I noticed that boost::thread constructor makes a copy of its argument. Suppose I have a big structure that I use as thread context and I don't want redundant copies on stack or anywhere else. </p> <p> For example: </p> <p> struct foo { </p> <blockquote> <p> void operator()() { </p> <blockquote> <p> <em> zzz </em></p> </blockquote> <p> } </p> </blockquote> <p> private: </p> <blockquote> <p> foo(foo&amp;); <em> no copying, please </em></p> </blockquote> <p> }; </p> <p> boost::thread(foo()); <em> this fails due to private copy constructor </em></p> <p> I bumped into this problem when I included boost::thread itself into the structure (forbids copying too ;). </p> <p> Thus I wonder if the argument could be replaced by a reference to an argument which would not insist on copying things. </p> <p> As a workaround I do the following now: </p> <p> void bar(foo *ptr) { (*ptr)(); } boost::thread(boost::bind(bar, new foo)); </p> <p> I looked at repository on the web and I could not find class thread anymore (moved somewhere else since 1.35?), so maybe it's already fixed... </p> <p> Thanks for great library! </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/2144 Trac 1.4.3 mmikucionis@… Thu, 24 Jul 2008 14:04:15 GMT <link>https://svn.boost.org/trac10/ticket/2144#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2144#comment:1</guid> <description> <p> Argh! I just now realized that by this copy constructor one makes sure that foo() does not disappear after the thread is constructed. </p> <p> Sorry for the noise. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Anthony Williams</dc:creator> <pubDate>Fri, 25 Jul 2008 08:06:53 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/2144#comment:2 https://svn.boost.org/trac10/ticket/2144#comment:2 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">wontfix</span> </li> </ul> <p> Yes, as you noted the copying ensures the object lives as long as the thread. </p> <p> If you need to use a large, non-copyable object you can either use a pointer (as in your example), or use boost::ref: </p> <p> foo large_object; boost::thread t(boost::ref(large_object)); </p> <p> If you do this it is up to you to ensure that the referenced object outlives the thread. </p> Ticket