0

Good afternoon,

I've been attempting to create a handful of threads to execute an update loop on some data, but when I attempt to call std::thread newThread(updateLoop), I get a generic compiler error for:

"No instance of constructor 'std::thread::thread' matches the argument list,"

and a MS VC++ 2015 error C3867:

"'projectileHandeler::updateLoop': non-standard syntax; use '&' to create a pointer to member"

When I attempt to follow the recommendation in C3867, changing the threading call to std::thread newThread(&updateLoop) the compiler throws MS VC++ C2276:

"'&': illegal operation on bound member function expression."

After reading the compiler error resources and trying variations such as std::thread newThread(this->updateLoop), std::thread newThread(*updateLoop), and std::thread newThread(*&updateLoop) [I was getting desperate then... I knew that last one wasn't going to work... ], I still get various errors relating to either an improper reference or my call the std::thread not matching the any overloads.

Could anyone please shed some light on my mistakes, please?

Of course, the code:

ProjectHandeler.cpp: NB: The standard library thread and vector headers are included in stdafx.h

#include "stdafx.h"
#include "DataTypes.h"
#include "ProjectHandeler.h"

projectileHandeler::projectileHandeler(projectile* inputList[], int inputCount) {
    for (int i = 0; i < inputCount; i++) {
        projectileList.push_back(*inputList[i]);
    }

    //Create 1 thread for each 10 projectiles.
    for (unsigned int i = 0; i < projectileList.size(); i++) {
        std::thread thread(updateLoop);
        thread.detach();
    }
}

void projectileHandeler::updateLoop() {
//Do stuff
}

ProjectHandeler.h:

#pragma once
#ifndef PROJECTILE_H
#define PROJECTILE_H

#include "stdafx.h"
#include "DataTypes.h"


#endif

class projectileHandeler {
private:

    std::vector<projectile> projectileList;

    void updateLoop();

public:

    projectileHandeler(projectile* inputList[], int inputCount);

    ~projectileHandeler();

    projectile* getProjectilePointerFromId(unsigned int id);

    //Make the NPC and Player handeler friend classes

};
Vera F W C
  • 208
  • 3
  • 11

1 Answers1

0

Solution found... Although I still can't make the original version work, studying the post suggested by Niall had an obscure mention to using lambdas to preform the threading, which bypasses the need for member function references.

Therefore std::thread newThread(updateLoop); becomes std::thread([this] { updateLoop(); });

The later version also has the benefit of starting the thread anonymously,allowing the loop to execute with out having to rename anything.

That said, I would still appreciate an explanation to why the original code doesn't compile. After reading through it, I couldn't find an answer for the specific case here in the suggested duplicate post.

Vera F W C
  • 208
  • 3
  • 11
  • `std::thread thread(&projectileHandeler::updateLoop, this);`? – Niall Feb 01 '16 at 07:20
  • Oh... *facepalm* the reason that wasn't working was I typed 'std::thread thread(&ProjectileHandeler::updateLoop, this);' with a capital P.... That'll teach me to get sloppy with my class naming conventions. Thanks for the help! – Vera F W C Feb 01 '16 at 07:24