Boost线程 - 通过引用传递参数 [英] Boost threads - passing parameters by reference
本文介绍了Boost线程 - 通过引用传递参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的应用程序有一个类似于以下代码的部分
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是空的,当向量在堆栈上创建时,它的工作原理,当它在堆上创建我希望上面的代码工作,因为向量仍然在作用域,即使当它被创建在堆栈。
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))
这篇关于Boost线程 - 通过引用传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文