我一直在尝试 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, …Run Code Online (Sandbox Code Playgroud)