问题描述
我是第一次使用std :: async和std :: future并收到错误消息,请问我可以解决此问题。
错误:
** terminate called after throwing an instance of 'std::future_error'
what(): std::future_error: No associated state**
代码:
long long best_move(int depth,int start_score,char state[8][9],int state_score[8][8],long long alpha,long long beta){
long long curr = 0;
for(int i=0; i<8; ++i){
for(int j=0; j<8; ++j){
curr += state_score[i][j];
}
}
if(depth == 5){
//printf("Return value in the last node = %lld\n",current_score);
return curr;
}
// TURN OF THE PLAYER THAT MAXIMIZES THE score :
int mid[] = {0,7,1,6,2,5,3,4};
if(depth % 2 == 0){
//printf("Reached the maximizing player\n");
long long ans = -1e8;
for(int i=0; i<8; ++i){
for(int j=0; j<8; ++j){
if(isUpper(state[i][mid[j]]) && state_score[i][mid[j]]>0){
vector<pair<int,int>> possible_moves = get_moves(i,mid[j],state);
if(possible_moves.size() == 0) continue;
int co = -1;
std::future<long long> fut[possible_moves.size()];
for(auto u : possible_moves){
co = co + 1;
int x = u.first,y = u.second;
long long removed_points = state_score[x][y];
char state_copy[8][9];
int state_score_copy[8][8];
for(int k=0; k<8; ++k){
for(int io=0; io<9; ++io){
if(io!=8) state_score_copy[k][io] = state_score[k][io];
state_copy[k][io] = state[k][io];
}
}
long long new_score = curr - state_score[x][y];
if(depth >= 3 && new_score == start_score) continue;
state_copy[x][y] = state_copy[i][mid[j]];
state_copy[i][mid[j]] = '_';
state_score_copy[x][y] = state_score_copy[i][mid[j]];
state_score_copy[i][mid[j]] = 0;
//long long res = best_move(depth+1,state_copy,state_score_copy,alpha,beta);
fut[co] = std::async (std::launch::async,best_move,depth+1,start_score,beta);
//long long res = best_move(depth+1,beta);
//printf("%lld\n",res);
}
co = 0;
for(co = 0; co < possible_moves.size(); ++co){
long long res = fut[co].get();
ans = max(ans,res);
alpha = max(alpha,ans);
if(beta <= alpha) break;
if(depth == 0 && ans == res){
printf("got the answer = %lld\n",ans);
moveX = i;
moveY = mid[j];
toX = possible_moves[i].first;
toY = possible_moves[i].second;
}
}
}
}
}
return ans;
}
else{
long long ans = +1e8;
//printf("Reached the minimizing player\n");
for(int i=0; i<8; ++i){
for(int j=0; j<8; ++j){
if(isLower(state[i][mid[j]]) && state_score[i][mid[j]]<0){
vector<pair<int,y = u.second;
long long removed_points = state_score[x][y];
char state_copy[8][9];
int state_score_copy[8][8];
for(int k=0; k<8; ++k){
for(int io=0; io<9; ++io){
if(io!=9) state_score_copy[k][io] = state_score[k][io];
state_copy[k][io] = state[k][io];
}
}
long long new_score = curr - state_score[x][y];
if(depth >= 3 && new_score == start_score) continue;
state_copy[x][y] = state_copy[i][mid[j]];
state_copy[i][mid[j]] = '_';
state_score_copy[x][y] = state_score_copy[i][mid[j]];
state_score_copy[i][mid[j]] = 0;
//long long res = best_move(depth+1,beta);
//ans = min(ans,res);
//beta = min(res,beta);
//if(beta <= alpha) break;
}
co = 0;
for(co = 0; co < possible_moves.size(); ++co){
long long res = fut[co].get();
ans = min(ans,res);
beta = min(res,beta);
if(beta <= alpha) break;
}
}
}
}
return ans;
}
}
解决方法
您似乎正在尝试获得统一未来的价值。
这是由于您的内部循环中的这一行代码所致:
if (depth >= 3 && new_score == start_score) continue; // skips call to async().
此行表示无法保证fut[]
将持有possible_moves.size()
初始化的期货。
为确保您没有未初始化的期货,可以将期货存储在向量中。
如
//...
std::vector<std::future<long long>> fut;
fut.reserve(possible_moves.size());
//...
fut.push_back(std::async(/* ... */));
//...
for (size_t i = 0; i < fut.size(); ++i)
{
long long res = fut[i].get();
ans = min(ans,res);
beta = min(res,beta);
if (beta <= alpha) break;
}