0
votes

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

#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

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'

2
This sounds like an XY problem... What are you really trying to achieve?JHBonarius

2 Answers

1
votes

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.

0
votes
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. Be aware that member functions in reality accept a reference, although this, inside the function, is defined as pointer, thus dereferencing the this-pointer...

Sometimes, a lambda can be useful, which looks like this here:

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

Sure, overkill in given case, but might be useful in other situation sometime in future...