dimanche 1 décembre 2019

Attempt to use a deleted function with C++11 threads

I am trying to create a ThreadPool class which has a constructor like this

    ThreadPool(size_t numberOfThreads)
    : workerThreads(numberOfThreads) {
      workerThreads.reserve(numberOfThreads);
      for(int i =0; i < numberOfThreads; i++) {
        workerThreads.emplace_back(std::thread(&ThreadPool::doJob, this));
      }
    }

This fails to compile for me ans throws following error

error: attempt to use a deleted function
    __invoke(_VSTD::move(_VSTD::get<1>(__t)), _VSTD::move(_VSTD::get<_Indices>(__t))...);
    ^
/Library/Developer/CommandLineTools/usr/include/c++/v1/thread:352:5: note: in instantiation of function template specialization
      'std::__1::__thread_execute<std::__1::unique_ptr<std::__1::__thread_struct, std::__1::default_delete<std::__1::__thread_struct> >, void
      (ThreadPool::*)(std::__1::function<void (char *, std::__1::list<std::__1::basic_string<char>,
      std::__1::allocator<std::__1::basic_string<char> > > *)>), ThreadPool *, 2>' requested here
    __thread_execute(*__p, _Index());

As per other answers to similar question I also tried doing this

        workerThreads.emplace_back(std::thread(&ThreadPool::doJob, std::ref(*this)));

This is reproducing same issue as well. I am compiling my code with clang c++14 on macos

Aucun commentaire:

Enregistrer un commentaire