How can I use boost thread with C++11 lambda?
The following code doesn't work:
int sum;
m_workerThread=new boost::thread([]()
{
for(int i=0;i<100;i++)
{
sum=sum+i;
}
}
);
I am getting compile error.
Error 4 error C3493: 'sum' cannot be implicitly captured because no default capture mode has been specified
How can I fix this?
As per the error, just need to capture sum
. As-is, the lambda doesn't know what sum
is:
m_workerThread = new boost::thread([&sum]()
// ^^^^
{
for(int i=0;i<100;i++)
{
sum=sum+i;
}
}
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With