Precautions when passing std::thread parameters including references

The code snippet is as follows:

...
void TestFunc(int& data)
{
    
    
	...
}

int data = 0;
std::thread oneThread(testFunc, data); // 方式1 error
std::thread oneThread(TestFunc, std::ref(data)); // 方式2 ok

...

When configured std::threadwhen reference object needs to be passed, directly using Embodiment 1 will compile error , it should be used Embodiment 2 through std::ref()the package to achieve the purpose of transmitting reference.

Guess you like

Origin blog.csdn.net/xp178171640/article/details/111589422