samedi 7 novembre 2020

C++ error in creating threads, static assertion failed: std::thread arguments must be invocable after conversion to rvalues

The error message is below

/usr/include/c++/9/thread: In instantiation of ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (&)(DisplayObject&); _Args = {DisplayObject&}; <template-parameter-1-3> = void]’:
farmville.cpp:242:53:   required from here
/usr/include/c++/9/thread:120:44: error: static assertion failed: std::thread arguments must be invocable after conversion to rvalues
  120 |           typename decay<_Args>::type...>::value,
      |                                            ^~~~~
/usr/include/c++/9/thread: In instantiation of ‘struct std::thread::_Invoker<std::tuple<void (*)(DisplayObject&), DisplayObject> >’:
/usr/include/c++/9/thread:131:22:   required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (&)(DisplayObject&); _Args = {DisplayObject&}; <template-parameter-1-3> = void]’
farmville.cpp:242:53:   required from here
/usr/include/c++/9/thread:243:4: error: no type named ‘type’ in ‘struct std::thread::_Invoker<std::tuple<void (*)(DisplayObject&), DisplayObject> >::__result<std::tuple<void (*)(DisplayObject&), DisplayObject> >’
  243 |    _M_invoke(_Index_tuple<_Ind...>)
      |    ^~~~~~~~~
/usr/include/c++/9/thread:247:2: error: no type named ‘type’ in ‘struct std::thread::_Invoker<std::tuple<void (*)(DisplayObject&), DisplayObject> >::__result<std::tuple<void (*)(DisplayObject&), DisplayObject> >’
  247 |  operator()()

What I'm trying to do is to create a thread that controls an object. So I create a function which can control the object, and use the function to create a thread.

void chickenAction(DisplayObject &chicken){
    int x = std::max(1, chicken.current_x + (1 + std::rand()) % 10 - 5);
    int y = std::max(1, chicken.current_y + (1 + std::rand()) % 10 - 5);
    chicken.draw(y, x);
    chicken.moveto(y, x);
}

Below is the code to create the thread, chicken1 is an object:

std::thread chicken1Thread(chickenAction, chicken1);
chicken1Thread.join();

I think the below code can duplicate the error

#include <iostream>
#include <unistd.h>
#include <thread>

class DisplayThread {
 public:
  int size;
  DisplayThread(){};
  void changeSize(int x) {
      size = x;
  }
};

void displayAction(DisplayThread &td){
    td.changeSize(1);
}



int main() {
        DisplayThread td;
        std::thread displaytd(displayAction, td);
    displaytd.join();
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire