0

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?

2
  • What would be the return value of that function? a? And you want to retrieve it by reading b afterwards? Commented Mar 8, 2019 at 7:47
  • You pass arguments into a thread via boost::promise or boost::packaged_task and to get the result you use boost::future. (Or for simpler use use boot::async) Commented Mar 8, 2019 at 7:51

1 Answer 1

2

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)); 
Sign up to request clarification or add additional context in comments.

2 Comments

ahhh I need to add -lboost_ref in order for this to work?
no , you need to link with boost_thread and i suppose thread_system as well. There is no boost_ref library. DEMO