【发布时间】:2023-03-31 19:08:01
【问题描述】:
我一直在试验 std:thread。我正在使用二叉表达式树进行标准算术运算。我正在创建一个线程来执行计算并希望检查除以零。当线程以std::async 启动时,异常从工作线程中抛出并在主线程中被很好地捕获。当我用 std::thread 启动线程时,抛出异常时,我得到一个运行时错误,abort()。关于它为何与 std::async but notstd::thread` 一起使用的任何见解?
// Declaration in the Expression.h file
public:
static long double __stdcall ThreadStaticEntryPoint(void * pThis);
long double __stdcall Expression::ThreadStaticEntryPoint(void * pThis)
{
long double calc;
Expression* pthrdThis = (Expression*)pThis;
calc = pthrdThis->Calculate();
return calc;
}
case 6:
try {
// Below works when encountering divide by zero.
// The thrown exception is caught correctly
// Launch thread using the this pointer
std::future<long double> fu = std::async(std::launch::async,
ThreadStaticEntryPoint, this);
calc = fu.get();
// When Creating a thread as below and I throw a divide by zero
// exception I get an error in visual C++. Below does not work:
//std::thread t1(&Expresson::Calculate, this);
//t1.join();
// Below works fine also
//calc = Calculate();
}
catch (runtime_error &r)
{
ShowMessage("LoadMenu() Caught exception calling Calculate()");
ShowMessage(r.what());
}
catch (...) {
ShowMessage("Exception caught");
}
long double Expresson::Calculate()
{
Expression *e;
long double calc = 0;
e = rep->GetExpression();
if (e == NULL)
{
ShowMessage("Main Expression " + to_string(rep->GetMainExpIndex()) + " is NULL. ");
return 0;
}
calc = e->Calculate()
return calc;
}
//************************************************************
// Calculate - Calculates Lval / Rval, returns result
//************************************************************
long double Divide::Calculate()
{
Expression* lop = this->getLOperand();
Expression* rop = this->getROperand();
long double Lval = 0, Rval = 0;
long double result = 0;
if (lop == NULL || rop == NULL)
return result;
Lval = lop->Calculate();
Rval = rop->Calculate();
//result = divides<long double>()(Lval, Rval);
// The throw error below causes the error
if (Rval == 0)
throw runtime_error("DivExp::Calculate() - Divide by zero exception occured. Rval = 0");
result = Lval / Rval;
return result;
}
【问题讨论】:
-
你为什么期望它“工作”? (我把“工作”放在引号中是因为这个程序运行良好;它只是没有达到你的预期)
-
它完全符合所有情况的预期。只有当我抛出异常时,当使用 std::thread 启动线程时,我才会得到运行时 abort()。如果线程以 std::async 启动并且我抛出异常,则它会像我预期的那样被捕获在主线程中。为什么你说它没有达到我的预期?
-
因为你抛出异常的情况仍然是一种情况,在那种情况下它并没有达到你的预期......
-
好吧,你说得对。我似乎找不到解决方案,所以我在想我忽略了一些东西。顺便说一句,即使我像使用 std::thread 那样调用计算函数(即 std::future
fu = std::async(std::launch:: async, &Expression::Calculate, this); calc = fu.get();
标签: c++ multithreading c++11 stdthread