Multithreading c+中线程之间的异常传播+;0x
我正在使用gcc 4.5,希望将异常转移到其他线程: 似乎Multithreading c+中线程之间的异常传播+;0x,multithreading,exception-handling,c++11,Multithreading,Exception Handling,C++11,我正在使用gcc 4.5,希望将异常转移到其他线程: 似乎e在线程内部不是空的,但在线程外部却是空的?! 这里怎么了?我自己想出来的。线程首先创建可调用的结构的副本 以下工作如预期: #include <stdexcept> #include <iostream> #include <thread> int main() { std::exception_ptr e; std::thread t([&e]() {
e
在线程内部不是空的,但在线程外部却是空的?!
这里怎么了?我自己想出来的。线程首先创建可调用的结构的副本 以下工作如预期:
#include <stdexcept>
#include <iostream>
#include <thread>
int main()
{
std::exception_ptr e;
std::thread t([&e]()
{
try
{
throw std::runtime_error("Bad things happened");
}
catch (...)
{
std::cout << "caught" << std::endl;
e = std::current_exception();
if (e == NULL)
{
std::cout << "inside NULL" << std::endl;
}
}
});
t.join();
if (e == NULL)
{
std::cout << "outside NULL" << std::endl;
}
else
{
std::rethrow_exception(e);
}
return 0;
}
#包括
#包括
#包括
int main()
{
标准:例外情况;
标准::螺纹t([&e]()
{
尝试
{
抛出std::runtime_错误(“发生了不好的事情”);
}
捕获(…)
{
标准::cout
caught
outside NULL
#include <stdexcept>
#include <iostream>
#include <thread>
int main()
{
std::exception_ptr e;
std::thread t([&e]()
{
try
{
throw std::runtime_error("Bad things happened");
}
catch (...)
{
std::cout << "caught" << std::endl;
e = std::current_exception();
if (e == NULL)
{
std::cout << "inside NULL" << std::endl;
}
}
});
t.join();
if (e == NULL)
{
std::cout << "outside NULL" << std::endl;
}
else
{
std::rethrow_exception(e);
}
return 0;
}