为什么我在C ++中的异步,未来中会出错 [英] Why am I Getting error in async, future in C++

查看:161
本文介绍了为什么我在C ++中的异步,未来中会出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我第一次使用std :: async和std :: future并收到错误消息,请问我可以解决此问题吗?

I am using std::async and std::future for the first time and am getting the error, Can somone help me rectify it.

错误:

** 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, state_copy, state_score_copy, alpha, beta);
                        //long long res = best_move(depth+1, start_score, state_copy, state_score_copy, alpha, 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, 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!=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, state_copy, state_score_copy, alpha, beta);
                        fut[co] = std::async (std::launch::async, best_move, depth+1, start_score, state_copy, state_score_copy, alpha, beta);
                        //long long res = best_move(depth+1, start_score, state_copy, state_score_copy, alpha, 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;

    }

}

推荐答案

您似乎正在尝试获得统一未来的价值.

It looks like you are trying to get the value of an unitialized future.

这是由于您的内部循环中的这一行代码所致:

This is due to this line of code in your inner loop:

if (depth >= 3 && new_score == start_score) continue; // skips call to async().

此行表示不能保证 fut [] 将保存 possible_moves.size()初始化的期货.

This line means there is no guarantee that fut[] will hold possible_moves.size() initialized futures.

为确保没有未初始化的期货,可以将期货存储在向量中.

To ensure you do not have uninitialized futures, you could store your futures in a vector.

//...
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;  
 }

这篇关于为什么我在C ++中的异步,未来中会出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆