充分利用一个boost ::线程成员函数的返回值? [英] Getting return value from a boost::threaded member function?

查看:303
本文介绍了充分利用一个boost ::线程成员函数的返回值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个工人阶级像下面这样:

I have a worker class like the one below:

class Worker{
public:
  int Do(){
    int ret = 100;
    // do stuff
    return ret;
  }
}

这是为了用的boost ::线程执行和boost ::绑定,如:

It's intended to be executed with boost::thread and boost::bind, like:

Worker worker;
boost::function<int()> th_func = boost::bind(&Worker::Do, &worker);
boost::thread th(th_func);
th.join();

我的问题是,我怎么得到工人的返回值::做什么?

My question is, how do I get the return value of Worker::Do?

先谢谢了。

推荐答案

我不认为你可以得到的返回值。

I don't think you can get the return value.

相反,你可以将该值存储为工人中的一员:

Instead, you can store the value as a member of Worker:

class Worker{
public:
  void Do(){
    int ret = 100;
    // do stuff
    m_ReturnValue = ret;
  }
  int m_ReturnValue;
}

和使用它像这样:

Worker worker;
boost::function<void()> th_func = boost::bind(&Worker::Do, &worker);
boost::thread th(th_func);
th.join();
//do something with worker.m_ReturnValue

这篇关于充分利用一个boost ::线程成员函数的返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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