0

I'm trying to make a multithreaded initialization routine that is going to divvy up the task based on the number of concurentThreadsSupported. It's a constructor calling another member function, but no matter how I seem to format it, I can't seem to call the other member function as a thread function. So how would I go about properly threading a member function, with arguments, from inside a class?

And before it's asked, I'm not using "using namespace std;", instead, I'm using "using std::vector;" and others as required.

Universe::Universe(const unsigned __int16 & NumberOfStars, const UniverseType & Type, const UniverseAge & Age) { thread *t = new thread[concurentThreadsSupported-1]; for (unsigned __int32 i = concurentThreadsSupported - 1; i > 0; i--) { //problem line t[i] = thread(&Universe::System_Spawner, i, NumberOfStars / concurentThreadsSupported, Type, Age); } for (int i = concurentThreadsSupported - 1; i > 0; i--) { t[i].join(); cout << "Thread joined" << endl; } delete[] t; } void Universe::System_Spawner(const unsigned __int16 threadNumber, const unsigned __int16 NumberOfStars, const UniverseType & Type, const UniverseAge & Age) { cout << "Inside Thread" << endl; } 

The error that I get is

c:\program files (x86)\microsoft visual studio 14.0\vc\include\thr\xthread(238): error C2672: 'std::invoke': no matching overloaded function found

...

c:\program files (x86)\microsoft visual studio 14.0\vc\include\thr\xthread(238): error C2893: Failed to specialize function template 'unknown-type std::invoke(_Callable &&,_Types &&...)'

c:\program files (x86)\microsoft visual studio 14.0\vc\include\thr\xthread(238): note: With the following template arguments:

c:\program files (x86)\microsoft visual studio 14.0\vc\include\thr\xthread(238): note: '_Callable=void (__cdecl Universe::* )(unsigned short,unsigned short,const UniverseType &,const UniverseAge &)'

c:\program files (x86)\microsoft visual studio 14.0\vc\include\thr\xthread(238): note: '_Types={unsigned int, unsigned int, UniverseType, UniverseAge}'

1 Answer 1

2

All member functions of a class have this as an implicit first parameter. Generally when calling a member function the compiler will handle this for you, but when creating a new instance of std::thread you must do this yourself:

t[i] = thread(&Universe::System_Spawner, this, i, NumberOfStars / concurentThreadsSupported, Type, Age); 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! You don't want to know how long I struggled to find this out.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.