Home > database >  Need help using promise and future for the first time in C 14
Need help using promise and future for the first time in C 14

Time:07-22

i'm currently coding a chess game which includes a chess IA to play with. My chess IA working fine with minimax and alpha beta optimisation. But now, I would like to make it faster by using multithreading. It's my first time of using thread so I started first by running the minimax function on a single separate thread to test it out.

This is my code : `

std::function<std::pair<Move*, float>(Board, int, float, float, bool , bool)> task;

task = [&task](Board &board, int depth, float alpha, float beta, bool maximizingPlayer, bool maximizingColor) ->std::pair<Move*, float>

{ 
    if (depth == 0 || board.GameIsOver())
         return std::make_pair(nullptr, evaluate(board, maximizingColor));

     auto possibleMoves = board.AllMovePossible(maximizingPlayer);
     Move bestMove;
     if (maximizingPlayer) {
         float maxEval = INFINITY;
         for (PossibleMoves& p : possibleMoves) {
             for (auto& move : p.moves) {
                 board.Move(p.piece, move.first, move.second);
                 int currentEval = task(board, depth - 1, alpha, beta, false, maximizingColor).second;
                 board.UndoMove();
                 if (currentEval < maxEval) {
                     maxEval = currentEval;
                     bestMove = Move(p.piece, move);
                 }
                 beta = min(beta, currentEval);
                 if (beta <= alpha)
                     break;
             }
         }
         return std::make_pair(&bestMove, maxEval);
     }
     else {
         float maxEval = -INFINITY;
         for (PossibleMoves& p : possibleMoves) {
             for (auto& move : p.moves) {
                 board.Move(p.piece, move.first, move.second);
                 int currentEval = task(board, depth - 1, alpha, beta, true, maximizingColor).second;
                 board.UndoMove();
                 if (currentEval > maxEval) {
                     maxEval = currentEval;
                     bestMove = Move(p.piece, move);
                 }
                 alpha = max(alpha, currentEval);
                 if (beta <= alpha)
                     break;
             }
         }
         return std::make_pair(&bestMove, maxEval);
    }};

std::function<void(std::promise<std::pair<Move*, float>>, Board, int, float, float, bool, bool)> taskWorker;

taskWorker = [&task](std::promise<std::pair<Move*, float>> &&p, Board board, int depth, float alpha, float beta, bool maximizingPlayer, bool maximizingColor)
{std::pair<Move*, int> bestMove = task(board, depth, alpha, beta, maximizingPlayer, maximizingColor);
    p.set_value(bestMove);
};

std::promise<std::pair<Move*, float>> p;
auto f = p.get_future();

std::thread worker(taskWorker,std::move(p),board,depth, alpha, beta, maximizingPlayer, maximizingColor);

std::pair<Move*, int> bestMoveFromThread = f.get();

worker.join();

return bestMoveFromThread;`

To explain my code: the lambda function task is my minimax function, it is using recursion and returning the best move for a board past by reference.

Then the taskWorker is what the thread is going to do. It takes a promise in parameter to store the future best move and use the task function to find it. It also takes a copy of the board because i would like to have multiple thread later that doesn't play moves on the same board.

Then I set up my promise p and my future f and I launch the thread with the function taskWorker then wait for it to end to get the result store in future. But my problem is that f.get() doesn't have result.

Although the thread seems to work fine because when i debug it seems to find the best move and p.set-value(bestMove) change p to 'has-result' with the correct best move stored (screenshots of debugging linked).

So the problem seems to be between std::promise<std::pair<Move*, float>> p; and std::pair<Move*, int> bestMoveFromThread = f.get();

It's my first time using promise and future, so I simply copied examples I found on the internet.

Did I misunderstand something about promise and future ? Am I not using these correctly ?

Thanks for your answers. (sorry if that came out wrong, I am not native english speaker).

(debugging) promise in the thread

(debugging) promise out of the thread

CodePudding user response:

You're returning a pointer to a local variable (Move* from the task). So there's already big potential for unexpected issues. Also, in some places your pair contains an int, in others a float. That type confusing might explain the rest.

Generally you understand future/promise I think. But for this example you should look into std::async to replace the thread object.

  • Related