C++ future的其他成员

C++ future的其他成员,第1张

future.status枚举类型,

enum class future_status { // names for timed wait function returns
    ready,
    timeout,
    deferred
};
using namespace std;

int mythread() {
    cout << "thread id = " << this_thread::get_id() << endl;
    chrono::milliseconds drue(5000);
    std::this_thread::sleep_for(drue);
    cout << "thread id = " << this_thread::get_id() << endl;
    return 5;
}
int main()
{
    cout << "main id" << this_thread::get_id() << endl;
    future result = async(mythread);
    cout << "countinue......" << endl;
    //cout << result.get() << endl;
    future_status statu = result.wait_for(std::chrono::seconds(1));
    if (statu == std::future_status::timeout) {
        cout << "超时,子线程还没有执行完毕" << endl;
    }
    cout << "i love china" << endl;
    return 0;
}

std::future.wait_for()等待子线程的时间,如果在等待时间内子线程没有结束,则返回std::status::timeout。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/634721.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-16
下一篇 2022-04-16

发表评论

登录后才能评论

评论列表(0条)

保存