Home Dashboard Directory Help

std::thread does not accept std::move by James K Edwards


 as Fixed Help for as Fixed

Sign in
to vote
Type: Bug
ID: 737812
Opened: 4/19/2012 8:58:36 PM
Access Restriction: Public
Moderator Decision: Sent to Engineering Team for consideration
User(s) can reproduce this bug


This line fails to compile.

    std::thread th(&ThFun, std::move(prms));

std::bind requires copyable, but std::thread does not.
Sign in to post a comment.
Posted by Microsoft on 5/7/2012 at 6:34 PM

Thanks for reporting this bug. I've resolved it as Fixed because this compiles with my current build of VC11 (and you'll observe this in VC11 RTM).

However, this area is fragile, and we didn't intentionally fix this. The Connect bug http://connect.microsoft.com/VisualStudio/feedback/details/729886/std-thread-constructor-doesnt-handle-movable-object (internally DevDiv#377755) tracks the ultimate problem here: we used std::bind() to implement std::thread, but the Standard actually forbids that because they have different semantics. In particular, bound arguments are supposed to be presented as lvalues to bind() functors (so they can be invoked repeatedly), but as rvalues to thread functors.

Your code compiles because you're moving prms into the thread, and then having ThFun() take an lvalue reference. Very similar code, taking the promise by value or by rvalue reference, will not compile with VC11 (but should, according to C++11).

In the future, we will attempt to fix this, by reimplementing std::thread so it doesn't use std::bind(). However, we won't have enough time to do this for VC11 RTM.

If you have any further questions, feel free to E-mail me at stl@microsoft.com .

Stephan T. Lavavej
Visual C++ Libraries Developer
Posted by MS-Moderator10 [Feedback Moderator] on 4/20/2012 at 2:11 AM
Thanks for your feedback.

We are rerouting this issue to the appropriate group within the Visual Studio Product Team for triage and resolution. These specialized experts will follow-up with your issue.
Posted by MS-Moderator01 on 4/19/2012 at 9:54 PM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(http://support.microsoft.com)
Sign in to post a workaround.
Posted by James K Edwards on 4/19/2012 at 8:59 PM
You can use std::ref, but it's not the same.