|
|
|
|
|
|
|
|
//请打开结构化异常(/EHa)
|
|
|
//请打开结构化异常(/EHa)
|
|
|
auto async_signal_exception(const intptr_t dividend)
|
|
|
auto async_signal_exception(const intptr_t dividend)
|
|
|
{
|
|
|
{
|
|
|
using namespace std::chrono;
|
|
|
|
|
|
|
|
|
|
|
|
resumef::awaitable_t<int64_t> awaitable;
|
|
|
|
|
|
|
|
|
awaitable_t<int64_t> awaitable;
|
|
|
|
|
|
|
|
|
std::thread([dividend, st = awaitable._state]
|
|
|
std::thread([dividend, st = awaitable._state]
|
|
|
{
|
|
|
{
|
|
|
std::this_thread::sleep_for(50ms);
|
|
|
|
|
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
|
|
try
|
|
|
try
|
|
|
{
|
|
|
{
|
|
|
|
|
|
//也可以注释掉这个判断,使用结构化异常。但就获得不了具体描述信息了
|
|
|
if (dividend == 0)
|
|
|
if (dividend == 0)
|
|
|
throw std::logic_error("divided by zero");
|
|
|
throw std::logic_error("divided by zero");
|
|
|
st->set_value(10000 / dividend);
|
|
|
st->set_value(10000 / dividend);
|
|
|
|
|
|
|
|
|
return awaitable;
|
|
|
return awaitable;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
auto async_signal_exception2(const intptr_t dividend)
|
|
|
|
|
|
{
|
|
|
|
|
|
awaitable_t<int64_t> awaitable;
|
|
|
|
|
|
|
|
|
|
|
|
std::thread([dividend, st = awaitable._state]
|
|
|
|
|
|
{
|
|
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
|
|
|
|
|
if (dividend == 0)
|
|
|
|
|
|
st->throw_exception(std::logic_error("divided by zero"));
|
|
|
|
|
|
else
|
|
|
|
|
|
st->set_value(10000 / dividend);
|
|
|
|
|
|
}).detach();
|
|
|
|
|
|
|
|
|
|
|
|
return awaitable;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
future_vt test_signal_exception()
|
|
|
future_vt test_signal_exception()
|
|
|
{
|
|
|
{
|
|
|
for (intptr_t i = 10; i >= 0; --i)
|
|
|
for (intptr_t i = 10; i >= 0; --i)
|
|
|
{
|
|
|
{
|
|
|
try
|
|
|
try
|
|
|
{
|
|
|
{
|
|
|
auto r = co_await async_signal_exception(i);
|
|
|
|
|
|
|
|
|
auto r = co_await async_signal_exception2(i);
|
|
|
std::cout << "result is " << r << std::endl;
|
|
|
std::cout << "result is " << r << std::endl;
|
|
|
}
|
|
|
}
|
|
|
catch (const std::exception& e)
|
|
|
catch (const std::exception& e)
|