升压线程 - 通过引用传递参数 [英] Boost threads - passing parameters by reference
本文介绍了升压线程 - 通过引用传递参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的应用程序有类似以下code段
My application has a section that resembles the following code
void SomeClass::OtherMethod(std::vector<std::string>& g)
{
g.pushback("Something");
}
void SomeClass::SomeMethod()
{
std::vector<std::string> v;
boost::thread t(boost::bind(&SomeClass::OtherMethod,this,v)
t.join();
std::cout << v[0]; //Why is this empty when the vector created on stack
}
我想知道为什么在栈上创建矢量时,矢量V是空的,在堆上创建时,它的工作原理。我期待上述code的工作,因为即使是在栈上创建的矢量保持在范围内。
I wanted to know why the vector v is empty when the vector is created on the stack and it works when it is created on the heap. I was expecting the above code to work since the vector remains in scope even when it is created on the stack.
推荐答案
绑定
将其参数。使用的boost :: REF
:
boost::thread t(boost::bind(&SomeClass::OtherMethod,this, boost::ref(v))
这篇关于升压线程 - 通过引用传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文