【发布时间】:2020-05-05 03:09:47
【问题描述】:
我想异步运行我的 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)>'
我最终希望在每个可能的情况下都在单独的线程上运行该算法,或者一次在两个线程上运行该算法,看看它是否能给我带来性能提升。
最少的代码:
#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);
}
【问题讨论】:
-
你还记得
#include <future>吗? -
你遇到了什么错误?
-
@JesperJuhl 是的,我包括了未来和线程。
-
在询问构建错误时,请始终包含 full 和 complete 错误输出,因为它通常包含有关问题可能是什么的提示.将错误输出复制粘贴(作为文本!)到问题中。也请花点时间刷新how to ask good questions,以及this question checklist。
标签: c++ multithreading asynchronous future