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

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

问题描述

我正在与升压::绑定和创建线程的一些问题。

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.

所以,如果你想打电话:

So if you wanted to call:

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

祝你好运。

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

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