0

I have been trying to construct a thread using class members. I am unable to determine why the compiler (VS2015) keeps complaining.
I have a class with two member functions. Load(...) takes in arguments by reference and is public. While ProcessQ(...) is private ,takes arguments by reference. ProcessQ is passed onto the thread. But I keep getting the error

"unknown-type std::invoke(_Callable &&,_Types &&...)" at the constructor

thread(&FileProcessor::ProcessQ, ref(fileQ), ref(fpQ), folder);

Seems that I have missed out constructor syntax of thread?

typedef vector<string> FPTemplate;
class FileProcessor
    {
    public:
        void Load(string folder, concurrent_queue<string>& fileQ, concurrent_queue<FPTemplate>& fpQ);
    private:
        void ProcessQ(concurrent_queue<string> &fileQ, concurrent_queue<FPTemplate> &fpQ, string folder);
    };

And here is my Load(...) function

void FileProcessor::Load(string folder,concurrent_queue<string>& fileQ, concurrent_queue<FPTemplate>& fpQ)
        {
            // Create threads and exectue
            const short nThreads = 8;
            thread fileProcessorThreads[nThreads];

            for (short i = 0; i < nThreads; i++)
            {
                fileProcessorThreads[i] = thread(&FileProcessor::ProcessQ, ref(fileQ), ref(fpQ), folder);
            }
            for (auto& th : fileProcessorThreads) 
            {
                th.join();
            }
        }   
Wajih
  • 793
  • 3
  • 14
  • 31
  • `ProcessQ` is a member function, you'll need to pass `this` or `*this` as the [second argument](http://en.cppreference.com/w/cpp/concept/Callable) to `thread`'s constructor. – user657267 Aug 20 '15 at 05:45
  • Don't know if this helps but you have an extra '}' at the end of your class declaration, and make the callback static –  Aug 20 '15 at 05:45
  • @AaryamanSagar - typo error! Fixed it – Wajih Aug 20 '15 at 05:46
  • @user657267 I did not understand it clearly, could you write the line please? – Wajih Aug 20 '15 at 05:48
  • @Wajih `thread(&FileProcessor::ProcessQ, this, ref(fileQ), ref(fpQ), folder);` – user657267 Aug 20 '15 at 05:49
  • @user657267 Thanks, it worked. Phew this c++ thing is so hard sometimes! – Wajih Aug 20 '15 at 05:52
  • @user657267 Post as an answer so that I can mark it. – Wajih Aug 20 '15 at 05:55
  • @Wajih No problem, [cppreference](http://en.cppreference.com/w/) is a an excellent resource if you don't use it already, you should also try searching older questions on SO before asking. I can't post an answer as the question has (correctly) been marked as a duplicate. – user657267 Aug 20 '15 at 05:55
  • Oh well! definitely going through cppreference. And thanks for the answers. Bad luck it is a duplicate! – Wajih Aug 20 '15 at 06:06

0 Answers0