[英]How do I use boost.lambda with boost.thread to get the thread's return value?
[英]How do I get return values from a boost thread?
我想做一些簡單的事情
void returnVal(int a, int &b)
{
b = a;
}
int main()
{
int b = 0;
boost::thread t(returnVal,1,b);
t.join();
}
這會引發錯誤。 無法將int&轉換為int。 必須有一種簡單的方法來獲取boost中的返回值,如果沒有,則沒有人對原因有一個恰當的解釋?
boost::thread
構造函數使用boost::bind
。 boost :: bind默認通過復制獲取參數,因此
b = a;
修改main中b
副本。 您需要使用boost::ref()
將對b
引用傳遞給thread
:
boost::thread t(returnVal,1,boost::ref(b));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.