Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/135.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 从类内部线程化成员函数_C++_Multithreading_Class_C++11_Visual Studio 2015 - Fatal编程技术网

C++ 从类内部线程化成员函数

C++ 从类内部线程化成员函数,c++,multithreading,class,c++11,visual-studio-2015,C++,Multithreading,Class,C++11,Visual Studio 2015,我正在尝试创建一个多线程初始化例程,该例程将根据支持的ConcurentThreads的数量划分任务。它是一个调用另一个成员函数的构造函数,但无论我如何格式化它,我似乎都不能将另一个成员函数作为线程函数调用。那么,我该如何从类内部正确地处理带有参数的成员函数的线程呢 在询问之前,我没有使用“usingnamespace std;”,而是使用了“using std::vector;”和其他需要的名称 Universe::Universe(const unsigned __int16 & N

我正在尝试创建一个多线程初始化例程,该例程将根据支持的ConcurentThreads的数量划分任务。它是一个调用另一个成员函数的构造函数,但无论我如何格式化它,我似乎都不能将另一个成员函数作为线程函数调用。那么,我该如何从类内部正确地处理带有参数的成员函数的线程呢

在询问之前,我没有使用“usingnamespace std;”,而是使用了“using std::vector;”和其他需要的名称

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;
}
Universe::Universe(常数无符号uu int16和NumberOfStars,常数UniverseType和Type,常数UniverseAge和Age)
{
线程*t=新线程[concurentThreadsSupported-1];
对于(unsigned _int32 i=concurentThreadsSupported-1;i>0;i--)
{
//问题线
t[i]=线程(&Universe::System_Spawner,i,NumberOfStars/ConcurrentThreadsSupported,Type,Age);
}
对于(int i=concurentThreadsSupported-1;i>0;i--)
{
t[i].join();

cout类的所有成员函数都将
作为隐式第一个参数。通常,在调用成员函数时,编译器将为您处理此问题,但在创建
std::thread
的新实例时,您必须自己执行此操作:

t[i] = thread(&Universe::System_Spawner, this, i, NumberOfStars / concurentThreadsSupported, Type, Age);

类的所有成员函数都将
this
作为隐式第一个参数。通常,在调用成员函数时,编译器会为您处理此问题,但在创建
std::thread
的新实例时,您必须自己执行此操作:

t[i] = thread(&Universe::System_Spawner, this, i, NumberOfStars / concurentThreadsSupported, Type, Age);

谢谢!你不想知道我花了多长时间才找到答案。谢谢!你不想知道我花了多长时间才找到答案。