简体   繁体   English

线程中的成员函数指针

[英]Member-Function Pointer in Threads

I have made a class which uses an array of member-functions to initialize an array of threads. 我做了一个使用成员函数数组初始化线程数组的类。

I do not know how to pass the function pointer to the thread constructor. 我不知道如何将函数指针传递给线程构造函数。 There is few documentation about this topic. 关于此主题的文档很少。

class.h class.h

#define N_FUNCTIONS 23
class TradingData
{
public:
  void EXECUTE();

  void Book();
  void Charts();
  void Company();
  void Dividends();
  void Earnings();
  void EffectiveSpread();
  void Financials();
  void KeyStats();
  void LargestTrades();
  void List();
  void Logo();
  void News();
  void OHLC();
  void Peers();
  void Previous();
  void Price();
  void Quote();
  void Relevant();
  void Splits();
  void TimeSeries();
  void VolumeByVenue();
  void S_Previous();
  void S_Symbols();

private:

  std::thread p_thread[N_FUNCTIONS];

  typedef void (TradingData::*overall)();
  overall p_overall[N_FUNCTIONS] = {
    &TradingData::Book,
    &TradingData::Charts,
    &TradingData::Company,
    &TradingData::Dividends,
    &TradingData::Earnings,
    &TradingData::EffectiveSpread,
    &TradingData::Financials,
    &TradingData::KeyStats,
    &TradingData::LargestTrades,
    &TradingData::List,
    &TradingData::Logo,
    &TradingData::News,
    &TradingData::OHLC,
    &TradingData::Peers,
    &TradingData::Previous,
    &TradingData::Price,
    &TradingData::Quote,
    &TradingData::Relevant,
    &TradingData::Splits,
    &TradingData::TimeSeries,
    &TradingData::VolumeByVenue,
    &TradingData::S_Symbols,
    &TradingData::S_Previous
};

class.cpp class.cpp

void TradingData::EXECUTE()
{
    for (int i = 0; i < N_FUNCTIONS; i++) {
        p_thread[i] = std::thread((this->*p_overall[i])()); //here is the problem
    }
    for (int i = 0; i < N_FUNCTIONS; i++) {
        p_thread[i].join();
    }
    std::cout << "finished successfully" <<std::endl;
}

I got the next error: Error C2440 '': cannot convert from 'void' to 'std::thread' 我收到下一个错误:错误C2440”:无法从“无效”转换为“ std :: thread”

You should write call; 你应该写电话;

p_thread[i] = std::thread(TradingData::p_overall[i], this);

If you call a member function the class name will be include in the call. 如果调用成员函数,则类名称将包含在调用中。

p_thread[i] = std::thread((this->*p_overall[i])());

This will pass the return value of the member function being called to the thread constructor. 这会将被调用的成员函数的返回值传递给线程构造函数。 But as you do not return something callable, this will even fail to compile, of course. 但是,由于您不返回可调用的内容,因此,这当然甚至无法编译。

Be aware that the object on which you call a member function actually is passed (transparently for you) as first parameter to the function being called. 请注意,您实际上在调用成员函数的对象(对您来说是透明的)作为第一个参数传递给了被调用函数。 And this is what you need to reflect when creating the thread: 这是创建线程时需要反映的内容:

p_thread[i] = std::thread(p_overall[i], *this);

The thread upon starting will now call the member function with *this as first argument. 现在,启动时的线程将使用*this作为第一个参数来调用成员函数。 Be aware that member functions in reality accept a reference, although this , inside the function, is defined as pointer, thus dereferencing the this-pointer... 请注意,虽然函数内部的this定义为指针,但实际上成员函数会接受引用,从而取消引用this指针。

Sometimes, a lambda can be useful, which looks like this here: 有时,lambda可能很有用,如下所示:

std::thread t(this, i {(this->*p_overall[i])(); }); std :: thread t(this,i {(this-> * p_overall [i])();});

Sure, overkill in given case, but might be useful in other situation sometime in future... 当然可以,在特定情况下会造成过度杀伤,但将来在某些情况下可能会有用...

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

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