Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.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
Multithreading 创建带有类内参数的线程类函数_Multithreading_C++11_Stdthread - Fatal编程技术网

Multithreading 创建带有类内参数的线程类函数

Multithreading 创建带有类内参数的线程类函数,multithreading,c++11,stdthread,Multithreading,C++11,Stdthread,我有一个类为AI的对象,它有一个私有的非静态void函数,它有很多参数:minmax(double&val,double alpha,double beta,unsigned short depth,board*thistturn);因为这是一个非常耗时的函数,我想使用多个线程并发运行它,因此我必须在AI类中的另一个函数中创建一个线程,其中包含此函数 根据在包含非静态成员函数且没有所述类参数的成员函数内生成线程,必须调用: std::thread t(&myclass::myfunc,t

我有一个类为
AI
的对象,它有一个私有的非静态void函数,它有很多参数:
minmax(double&val,double alpha,double beta,unsigned short depth,board*thistturn)
;因为这是一个非常耗时的函数,我想使用多个线程并发运行它,因此我必须在AI类中的另一个函数中创建一个线程,其中包含此函数

根据在包含非静态成员函数且没有所述类参数的成员函数内生成线程,必须调用:

std::thread t(&myclass::myfunc,this);
根据函数线程,可以创建具有多个参数的函数:

std::thread t(foo,4,5) 
其中函数“foo”有2个整数参数

然而,我希望将这些东西混合在一起,从它所属的类内部调用一个具有参数的函数,它也是一个非静态成员函数,我不知道如何将这些东西混合在一起

我已经尝试过(记住它在AI类的函数中):

但这两种情况都会失败,出现如下编译错误:

error: no matching constructor for initialization of 'std::thread'
candidate constructor template not viable: requires single argument '__f', but
  7 arguments were provided
因此,我的问题是,如果不是,甚至可以从类的成员函数内部调用具有多个参数的非静态成员函数作为线程,如果是这种情况,如何实现

然而,这个问题不是在我的具体案例中使用多线程是否是一个好主意

编辑
在做了一些测试之后,我意识到我根本不能以线程的形式调用带有参数的函数,甚至不能直接从main调用只有一个参数的非成员函数,但是通过询问我在哪里意识到我只需要添加标志-std=c++11就可以解决这个问题(因为显然Mac并不总是使用所有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();
}
#包括
#包括
班级委员会;
类AI
{
公众:
void minmax(双倍&val、双倍alpha、双倍beta、无符号短深度、单板*本回合)
{

std::cout try
std::thread t(&AI::minmax,this,std::ref(val),alpha,beta,depth,thistturn,this);
@PiotrSkotnicki这样做会产生与以前相同的错误,并且这个新错误
没有匹配的函数来调用“\uu invoke”
显示代码,否则它永远不会显示代码happened@PiotrSkotnicki你有一个“这个”太多了。它应该读
std::thread t(&AI::minmax,this,std::ref(val),alpha,beta,depth,thistturn);
@pschill OP代码的复制粘贴,但可能您是对的,示例代码仍然会产生两个错误:首先:std::ref导致编译器错误:
错误:调用“\u invoke”时没有匹配的函数。
后接几行:
…包含在…
的文件中,但最后表明问题是
std::ref(val)
。其次,它导致旧的
没有匹配的构造函数来初始化'std::thread'候选构造函数模板不可行:需要单个参数'\uu f',但提供了7个参数
@Nikolaj我发布的代码在Visual Studio 2015中编译得很好(请参阅)。我这里没有VS2013来测试代码,早期版本将无法工作,因为那里缺少
#include
。您使用什么编译器?
error: no matching constructor for initialization of 'std::thread'
candidate constructor template not viable: requires single argument '__f', but
  7 arguments were provided
#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();
}