3

I have a function foo(myclass* ob) and I am trying to create a consumer thread using consumer_thread(boost::bind(&foo)(&ob))

The code does not compile which I believe is due to my inappropriate way of passing the function argument to the function pointer.

class myclass{
// stuff
}

void foo(myclass* ob){
// stuff
}

int main(){
myclass* ob = new myclass();
boost::thread consumer_thread()boost::bind(&foo)(&ob));
// stuff
}

What am I doing wrong? Can anyone here elaborate on boost::bind and how to pass function pointers with function arguments?

Thanks in advance!

ND_27
  • 764
  • 3
  • 8
  • 26

2 Answers2

3

Your code sample has some errors. This is a fixed version, where the return value of the call to bind is used as the sole parameter in the boost::thread constructor:

boost::thread consumer_thread(boost::bind(foo, ob));

But you can skip the call to boost::bind entirely, passing the function and its parameters to the constructor:

boost::thread consumer_thread(foo, ob);
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • Thanks.. I have some lack of clarity about when bind should or shouldn't be used, although I read up on it. – ND_27 Sep 30 '13 at 17:36
2

That should be bind(foo, ob).

However, I'm fairly sure that boost::thread has the same interface as std::thread, in which case you don't need bind at all:

boost::thread consumer_thread(foo, ob);
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644