在 class 中创建带有参数的线程 class 函数
create threaded class functions with arguments inside class
我有一个 class AI
的对象,它有一个私有的非静态 void 函数,它有很多参数:minmax(double& val,double alpha, double beta, unsigned short depth, board* thisTurn)
;因为这是一个非常耗时的函数,我想同时使用多个线程来 运行 它,因此我必须在 AI class;[=22= 的另一个函数中创建一个带有这个函数的线程]
根据This question要在包含非静态成员函数的成员函数内创建线程而没有上述 class 的参数,必须调用:
std::thread t(&myclass::myfunc,this);
并且根据 this tutorial 可以这样创建具有多个参数的函数线程:
std::thread t(foo,4,5)
其中函数 'foo' 有 2 个整数参数
但是我希望将它们混合在一起,从它所属的 class 内部调用一个具有参数的函数,该函数也是一个非静态成员函数,而我不是确定如何将这些混合到事物中。
我当然试过了(记住它在 AI class 的一个函数中):
std::thread t(&AI::minmax,val,alpha,beta,depth,thisTurn,this);
和
std::thread t(&AI::minmax,this,val,alpha,beta,depth,thisTurn);
但是这两种情况都失败了,编译错误如下:
error: no matching constructor for initialization of 'std::thread'
candidate constructor template not viable: requires single argument '__f', but
7 arguments were provided
因此,我的问题是,无论是否如此,甚至可以从 class 的成员函数内部调用一个具有多个参数的非静态成员函数作为一个线程,如果是这种情况,它是如何完成的。
然而,这个问题不是在我的特定情况下使用多线程是否是个好主意。
编辑
在做了一些测试后,我意识到我根本无法调用带参数的函数作为线程,甚至不能直接从 main 调用只有一个参数的非成员函数,但是通过询问 解决了这个问题我意识到我只需要添加标志 -std=c++11 (因为显然 macs 并不总是使用所有 c++11 功能作为标准)在这样做之后答案工作正常。
您需要使用 std::ref
包装引用。以下示例可以正常编译:
#include <iostream>
#include <thread>
class board;
class AI
{
public:
void minmax(double& val, double alpha, double beta, unsigned short depth, board* thisTurn)
{
std::cout << "minmax" << std::endl;
}
void spawnThread()
{
double val;
double alpha;
double beta;
unsigned short depth;
board* thisTurn;
std::thread t(&AI::minmax, this, std::ref(val), alpha, beta, depth, thisTurn);
t.join();
}
};
int main()
{
AI ai;
ai.spawnThread();
}
我有一个 class AI
的对象,它有一个私有的非静态 void 函数,它有很多参数:minmax(double& val,double alpha, double beta, unsigned short depth, board* thisTurn)
;因为这是一个非常耗时的函数,我想同时使用多个线程来 运行 它,因此我必须在 AI class;[=22= 的另一个函数中创建一个带有这个函数的线程]
根据This question要在包含非静态成员函数的成员函数内创建线程而没有上述 class 的参数,必须调用:
std::thread t(&myclass::myfunc,this);
并且根据 this tutorial 可以这样创建具有多个参数的函数线程:
std::thread t(foo,4,5)
其中函数 'foo' 有 2 个整数参数
但是我希望将它们混合在一起,从它所属的 class 内部调用一个具有参数的函数,该函数也是一个非静态成员函数,而我不是确定如何将这些混合到事物中。
我当然试过了(记住它在 AI class 的一个函数中):
std::thread t(&AI::minmax,val,alpha,beta,depth,thisTurn,this);
和
std::thread t(&AI::minmax,this,val,alpha,beta,depth,thisTurn);
但是这两种情况都失败了,编译错误如下:
error: no matching constructor for initialization of 'std::thread'
candidate constructor template not viable: requires single argument '__f', but
7 arguments were provided
因此,我的问题是,无论是否如此,甚至可以从 class 的成员函数内部调用一个具有多个参数的非静态成员函数作为一个线程,如果是这种情况,它是如何完成的。
然而,这个问题不是在我的特定情况下使用多线程是否是个好主意。
编辑
在做了一些测试后,我意识到我根本无法调用带参数的函数作为线程,甚至不能直接从 main 调用只有一个参数的非成员函数,但是通过询问
您需要使用 std::ref
包装引用。以下示例可以正常编译:
#include <iostream>
#include <thread>
class board;
class AI
{
public:
void minmax(double& val, double alpha, double beta, unsigned short depth, board* thisTurn)
{
std::cout << "minmax" << std::endl;
}
void spawnThread()
{
double val;
double alpha;
double beta;
unsigned short depth;
board* thisTurn;
std::thread t(&AI::minmax, this, std::ref(val), alpha, beta, depth, thisTurn);
t.join();
}
};
int main()
{
AI ai;
ai.spawnThread();
}