jeudi 9 janvier 2020

Cannot compile with thread that uses member function within same class

I am attempting to create a thread that calls a member function, from inside that same class.

I've seen some similar questions like this one.

However, mine does not seem to work. I get an error,

std::thread arguments must be invocable after conversion to rvalues

With this setup:

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();
    }
}

Here is the full error when I attempt to compile: (edited with generic names)

/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,
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

It could be reference related. Only the std::vector needs to be a reference, the rest can just be copies. Do I still need to wrap something in std::ref() for some reason? Or do I just have the syntax of std::thread wrong somehow? Do I need to be using "future" and async, for some reason?

Aucun commentaire:

Enregistrer un commentaire