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;
}
The lambda signature expects a reference. However, std::thread
can't accept a reference : it must be able to copy the objects into the function it calls.
The solution is std::reference_wrapper
and its handy little wrappers std::ref
and std::cref
.
Here's a working example :
#include <thread> // std::thread
#include <functional> // std::ref
void foo() {
int i_ = 0;
auto thread_func = [](int &i) {
return i+42;
};
// assertion failed: std::thread arguments must be invocable after conversion to rvalues
// std::thread t{thread_func, i_};
// OK!
std::thread t{thread_func, std::ref(i_)};
t.join();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.