简体   繁体   中英

How do I get return values from a boost thread?

I want to do something simple like

void returnVal(int a, int &b)
{
    b = a;
}

int main()
{
    int b = 0;
    boost::thread t(returnVal,1,b);
    t.join();
}

This throws errors. Cannot convert int& to int. There must be a simple way to get return values in boost, and if there is not does anyone have a decent explanation as to why?

boost::thread constructor uses boost::bind . boost::bind takes its arguments by copy default, so

b = a;

modifies copy of b from main. You need to use boost::ref() to pass reference to b into thread :

boost::thread t(returnVal,1,boost::ref(b));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM