c++11 - Passing by reference to std::thread c++0x in VS2012 -
void threadfn(int& i) { cout<<"hi thread "<<i<<endl; } int x = 0; void createthreads(vector<thread>& workers) { for(int = 0; i< 10; i++) { workers.push_back(thread(&threadfn, x)); } }
i expecting compilation error in thread creation (workers.push_back(thread(&threadfn, x));
) since x
should passed ref. though right syntax should've been:
workers.push_back(thread(&threadfn, std::ref(x)));
of course, code compiles fine , behaves properly. using vc11
. idea why isn't being flagged?
this vc11 bug, thread
object makes internal copies of arguments (as should) doesn't forward them threadfn
function correctly, happens reference binds thread
object's internal int
member.
gcc's std::thread
used have similar bug because used std::bind
implement it, replaced use of std::bind
different implementation detail forwards captured arguments function value.
Comments
Post a Comment