微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何将函数agruments指针,引用传递给“新线程”并期望返回值?

如何解决如何将函数agruments指针,引用传递给“新线程”并期望返回值?

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

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

导致错误错误:无法在未指定捕获认值的lambda中隐式捕获变量“游戏”

如何完成?

解决方法

要获取返回值,您可以使用std :: future和std :: async:

import CPromise from "c-promise2"; // npm package

function fetchWithTimeout(url,{timeout,...fetchOptions}= {}) {
    return new CPromise((resolve,reject,{signal}) => {
        fetch(url,{...fetchOptions,signal}).then(resolve,reject)
    },timeout)
}
        
const chain = fetchWithTimeout("https://run.mocky.io/v3/753aa609-65ae-4109-8f83-9cfe365290f0?mocky-delay=10s",{timeout: 5000})
    .then(request=> console.log('done'));
    
// chain.cancel(); - to abort the request before the timeout

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。