Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a boost thread with data?

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?

like image 780
Jack BeNimble Avatar asked Jan 23 '23 16:01

Jack BeNimble


1 Answers

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.

like image 166
Jere.Jones Avatar answered Jan 26 '23 04:01

Jere.Jones