0

I curently have an issue when using c++ thread. I have the following error :

/usr/include/c++/4.8/thread:140:47:   required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (TextMove::*)(); _Args = {}]’
textMove.cc:8:40:   required from here
/usr/include/c++/4.8/functional:1697:61: error: no type named ‘type’ in ‘class std::result_of<std::_Mem_fn<void (TextMove::*)()>()>’
       typedef typename result_of<_Callable(_Args...)>::type result_type;
                                                             ^
/usr/include/c++/4.8/functional:1727:9: error: no type named ‘type’ in ‘class std::result_of<std::_Mem_fn<void (TextMove::*)()>()>’
         _M_invoke(_Index_tuple<_Indices...>)

But my code seems to use correctly the functions :

class TextMove : Text{

public:
    TextMove(Viewport *v,rgb_matrix::VFont *f,rgb_matrix::Color *c,rgb_matrix::Color *bg,char* content,int extra) : Text(v,f,0,0,c,bg,content,extra){
        index = 0;
        limit = v->getX();
    }
    void play(int speed);
    void playThread(int speed);
private:
    void draw();
    int index;
    pthread_t thread;
    int limit;
};
void TextMove::play(int speed){
    std::thread t(&TextMove::playThread,speed);
    t.join();
}

void TextMove::playThread(int speed) {
    index = 0;
    while(_x>limit){
        draw();
        sleep(speed);
        _x--;
    }
}

I dont know if it's because I am using it in a class but I have not found the problem for now.

Samuel T
  • 11
  • 2
  • 4
    Does this answer your question? [Start thread with member function](https://stackoverflow.com/questions/10673585/start-thread-with-member-function) – 273K Jun 08 '21 at 07:18
  • 1
    you need to pass object pointer while launching thread using member function like this `std::thread t(&TextMove::playThread,this,speed);` – Smit Shah Jun 08 '21 at 07:22

1 Answers1

0

As pointed out by Smit Shah the comments, you can either:

  • Use the following std::thread constructor
template< class Function, class... Args >
explicit thread( Function&& f, Args&&... args );

In order to pass this in argument.

    std::thread t(&TextMove::playThread , this , speed);
    t.join();
  • Or you can simply pass a lambda when constructing the thread.
    std::thread t([this , &speed]{playThread(speed);});
    t.join();
Erel
  • 512
  • 1
  • 5
  • 14