简体   繁体   English

如何用数据创建一个boost线程?

[英]How to create a boost thread with data?

I'm running into some issues with boost::bind and creating threads. 我在使用boost :: bind和创建线程时遇到了一些问题。

Essentially, I would like to call a "scan" function on a "Scanner" object, using bind. 基本上,我想使用bind在“Scanner”对象上调用“扫描”功能。

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? 作为构造函数的一部分?

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: 因为我经常希望在创建绑定对象的对象上调用bind,所以我经常这样做:

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

Good luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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