Я пытаюсь создать поток, который вызывает функцию-член внутри того же класса.
Я видел несколько похожих вопросов, таких как этот .
Однако мой, похоже, не работает. Я получаю ошибку,
std::thread arguments must be invocable after conversion to rvalues
При такой настройке:
void MyClass::myFuncThread(
const unsigned threadID,
std::vector<myType*>& column,
const unsigned x,
const unsigned y) const
{
// Each thread will write once to a single unique index of the vector, which should work
// despite vector not being thread-safe
// Contents of this function omitted since it's not relevant to the error
}
void MyClass::myFunc(
std::vector<myType*>& column,
const unsigned x,
const unsigned y) const
{
column.resize(x, y);
// Create a vector of threads
std::vector<std::thread> threads;
threads.resize(numThreads);
// Launch all threads
for (unsigned i = 0; i < threads.size(); i++)
{
threads[i] = std::thread(
&MyClass::myFuncThread, this, i, column,
x, y);
}
// Join all the threads
for (unsigned i = 0; i < threads.size(); i++)
{
threads[i].join();
}
}
Вот полная ошибка при попытке компиляции: (отредактировано с именами generi c)
/usr/include/c++/8/thread: In instantiation of ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (MyClass*)(unsigned int, std::vector<myType*>&, unsigned int, unsigned int) const; _Args = {const MyClass*, unsigned int&, std::vector<myType*>&, const unsigned int&, const unsigned int&}; <template-parameter-1-3> = void]’:
myclass.cpp:100:23: required from here
/usr/include/c++/8/thread:120:17: error: static assertion failed: std::thread arguments must be invocable after conversion to rvalues
static_assert( __is_invocable<typename decay<_Callable>::type,
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
typename decay<_Args>::type...>::value,
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Это может быть ссылка. Только std::vector
должен быть ссылкой, остальные могут быть просто копиями. Мне все еще нужно что-то завернуть в std::ref()
по какой-то причине? Или у меня просто синтаксис std::thread
неправильный как-то? Нужно ли мне по каким-то причинам использовать «будущее» и asyn c?