Boost threads - passing parameters by reference

这一生的挚爱 提交于 2019-12-04 12:16:43

Bind copies its parameters. Use boost::ref:

boost::thread t(boost::bind(&SomeClass::OtherMethod,this, boost::ref(v))

A thread by default takes arguments by value, even if the function itself expects a reference. Use boost::ref() to force passing argument by reference.

() by default the arguments are copied into internal storage, where they can be accessed by the newly created thread of execution, even if the corresponding parameter in the function is expecting a reference.

A. Williams, "Concurrency in Action", 2.2 Passing arguments to a thread function.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!