如何将函数 agruments(指针,引用)传递给 "new thread" 并期望 return 值?

how to pass function agruments(pointer, reference) to a "new thread" and expect return value?

我正在尝试使用线程将从 class 的成员函数接收的指针传递给同一个 class 的另一个成员函数,并期望一个 return 值。

    class tictactoe
    {
    ...
    ..
    .
    };
    
    class AI {
    public:
        int pickSpot(tictactoe* game){
             t =  new thread([this]() {findOptions(game);} );
             return 0;
        }
        
        int findOptions(tictactoe* game){ return 0;}
    };

导致错误: 错误:无法在未指定捕获默认值的 lambda 中隐式捕获变量 'game'

怎么做到的?

要获得 return 值,您可以使用 std::future 和 std::async:

#include <future>
#include <thread>

int pickSpot(tictactoe* game){
    std::future<int> f = std::async(std::launch::async, [this, game]() { return findOptions(game); });
    f.wait();
    int res = f.get();
    return res;
}