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