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。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)