#include <iostream>
#include <future>
void work(std::promise<int> &promise){
try{
throw std::logic_error("hello");
}catch (...) {
promise.set_exception(std::current_exception());//获取当前的异常
}
}
void handle(std::future<int> &future){
try {
std::cout<<future.get()<<std::endl;
}catch (std::exception &e)
{
std::cout<<"exception "<<e.what()<<std::endl;//在另外的线程中处理异常
}
}
int main()
{
std::promise<int> promise;
std::future<int> future = promise.get_future();
std::thread t1(work,std::ref(promise));
std::thread t2(handle,std::ref(future));
t1.join();
t2.join();
return 0;
}
网友评论