views:

188

answers:

1

I'm running into some issues with boost::bind and creating threads.

Essentially, I would like to call a "scan" function on a "Scanner" object, using bind.

Something like this:

  Scanner scanner;
   int id_to_scan = 1;

   boost::thread thr1(boost::bind(&scanner::scan));

However, I'm getting tripped up on syntax. How do I pass the data into scan? As part of the constructor?

+8  A: 

Keep in mind that the first argument to any member function is the object.

So if you wanted to call:

scanner* s;
s->scan()

with bind you would use:

boost::bind(&scanner::scan, s);

If you wanted to call:

s->scan(42);

use this:

boost::bind(&scanner::scan, s, 42);

Since I often want bind to be called on the object creating the bind object, I often do this:

boost::bind(&scanner::scan, this);

Good luck.

Jere.Jones
I am not worthy :) Thank you!
Jack BeNimble