如何创建一个boost线程与数据? [英] How to create a boost thread with data?

查看:177
本文介绍了如何创建一个boost线程与数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一些与boost :: bind和创建线程有关的问题。

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.

像这样:

  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.

所以如果你想调用:

scanner* s;
s->scan()



with bind you would use:

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

如果您想呼叫:

s->scan(42);

使用:

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);

祝你好运。

这篇关于如何创建一个boost线程与数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆