C++ 如何在c++;?

C++ 如何在c++;?,c++,multithreading,asynchronous,future,C++,Multithreading,Asynchronous,Future,我想异步运行我的minimax算法,这样它就不会在等待时机到来时冻结ui。下面是我需要调用的静态方法: //ChessContext is the current state of the board //Turn contains fromX, fromY, toX, toY when moving a piece static Turn getBestTurn(ChessContext cc, int depth); 我试过这个: //context is a reference to t

我想异步运行我的minimax算法,这样它就不会在等待时机到来时冻结ui。下面是我需要调用的静态方法:

//ChessContext is the current state of the board
//Turn contains fromX, fromY, toX, toY when moving a piece
static Turn getBestTurn(ChessContext cc, int depth);
我试过这个:

//context is a reference to the game currently played
auto fu = std::async(std::launch::async, ChessContext::getBestTurn, context , 5);
Turn t = fu.get();
这给了我一个错误的说法

boardview.cpp:69:23: error: no matching function for call to 'async' 
future:1712:5: note: candidate template ignored: substitution failure [with _Fn = Turn (&)(ChessContext, int), _Args = <ChessContext &, int>]:
    no type named 'type' in 'std::result_of<Turn ((ChessContext, int))(ChessContext, int)>'
future:1745:5: note: candidate template ignored: substitution failure [with _Fn = std::launch, _Args = <Turn (&)(ChessContext, int), ChessContext &, int>]:
    no type named 'type' in 'std::result_of<std::launch (Turn ()(ChessContext, int), ChessContext, int)>'
boardview.cpp:69:23:错误:调用'async'没有匹配的函数
future:1712:5:注意:已忽略候选模板:替换失败[with _Fn=Turn(&)(ChessContext,int),_Args=]:
“std::result\u of”中没有名为“type”的类型
future:1745:5:注意:已忽略候选模板:替换失败[with _Fn=std::launch,_Args=]:
“std::result\u of”中没有名为“type”的类型
我最终想在一个单独的线程上运行算法,或者一次在两个线程上运行算法,看看它是否能提高性能

最小代码:

#include <iostream>
#include <thread>
#include <future> 

class Turn
{
};
class ChessContext 
{
public:
    ChessContext();
    ChessContext(ChessContext &cc);

    static Turn getBestTurn(ChessContext cc, int depth);
}; 

int main(){
    ChessContext context;
    auto fu = std::async(std::launch::async, ChessContext::getBestTurn, context, 5);
}
#包括
#包括
#包括
班级轮换
{
};
类ChessContext
{
公众:
ChessContext();
ChessContext(ChessContext&cc);
静态转向getBestTurn(ChessContext cc,int depth);
}; 
int main(){
语境;
auto-fu=std::async(std::launch::async,ChessContext::getBestTurn,context,5);
}
这是完整的项目

您的最小示例编译得很好

您的代码还包含以下内容:

    ChessContext(ChessContext &cc);
这不是一个普通的复制构造函数。复制构造函数通过
const
引用获取参数

下面是一个再现您的问题的最小示例:

#include <future>

struct A {
    A() {}
    A(A&) {}
};

void test(A) {}

int main() {
    A a;
    test(a); // OK
    std::async(std::launch::async, &test, a); // Not OK
}

您可以使用lambda使这里的生活更轻松:

auto fu = std::async(std::launch::async, [&]() { ChessContext::getBestTurn(context , 5); });

对于多线程,建议使用minimax的工作线程,但在实际场景中,由于您需要哈希表和其他内容,以允许强制移动或撤消移动等,因此不建议为每个深度搜索使用单独的线程。

ChessContext
不可复制
ChessContext(ChessContext&cc)
不是复制构造函数,您需要
ChessContext(const ChessContext&cc)

您是否记得
#包括
?您得到了什么错误?@JesperJuhl是的,我包括future和thread。当询问构建错误时,始终包括完整的错误输出,因为它通常包含关于问题可能是什么的提示。将错误输出复制粘贴(作为文本!)到问题中。另外,请花一些时间来刷新,以及。无法复制:请提供一个示例,代码在我正常运行时可以像这样工作:Turn-Turn=ChessContext::getBestTurn(context,5);语境。游戏回合(turn);我没注意到,非常感谢!现在该行是好的,但它在fu.get()上给了我这个错误;boardview.cpp:70:18:错误:从“void”到“Turn”的转换不可行。h:13:5:注意:候选构造函数不可行:无法将不完整类型的参数“void”转换为“const Turn&”,用于第一个参数这需要在lambda中返回,并且无论如何都不可能解决问题。您需要检查
fu
是否已准备就绪调用“<代码> FU.GET())/代码>无论如何,使用STD::未来不会帮助你,因为它将阻止主UI线程,考虑使用其他可行的选项,如STD::原子或STD::AtdialAlvia变量与STD::MutExchange没有注意到,非常感谢!
auto fu = std::async(std::launch::async, [&]() { ChessContext::getBestTurn(context , 5); });