从线程抛出异常并没有给出预期的结果
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从线程抛出异常并没有给出预期的结果相关的知识,希望对你有一定的参考价值。
在下面的代码片段中,我试图在重新投掷之后捕获异常,但无法实现相同。我不知道出了什么问题,因为我已经通过current_exception()保留了当前的teptr状态。线程在连续循环中运行,因此一旦其值达到大于2,则执行catch块并且控制到达循环之外但仍然如预期的那样我无法在第一次尝试中到达另一个catch块。
#include <boost/thread.hpp>
#include <boost/thread/scoped_thread.hpp>
#include <boost/chrono.hpp>
#include <iostream>
#include <boost/exception/all.hpp>
#include <exception>
using namespace std;
boost::exception_ptr teptr;
class myexception : public exception
{
virtual const char* what() const throw()
{
return "My exception happened";
}
} myex;
class ABC
{
public:
void start();
};
void ABC::start()
{
int i = 0;
cout << "running the thread" << std::endl;
while (1)
{
try
{
std::cout << "value of " << i << '
';
if (i > 2)
{
throw boost::enable_current_exception(myex);
}
i++;
}
catch (exception& e)
{
cout << "actual exception is" << e.what() << '
';
teptr = boost::current_exception();
break;
//throw myex;
}
}
}
int main()
{
ABC abc;
boost::thread thread_point;
while (1)
{
boost::thread thread_point;
thread_point = boost::thread(&ABC::start, abc);
if (teptr) {
try {
boost::rethrow_exception(teptr);
}
catch (const std::exception &ex)
{
std::cerr << "Thread exited with exception: " << ex.what() << "
";
exit(0);
}
}
}
}
答案
您的程序在没有同步的情况下同时从多个线程访问变量teptr
(以及myex
)。行为是未定义的。
更糟糕的是,你正在影响thread_point
并创建许多未加入的线程。你真的在运行无限的线程来共享相同的全局数据。
我想你真的在寻找future
s - 允许你从任何地方返回一个值或一个例外。所有异常处理魔法都为您完成:
#include <thread>
#include <future>
#include <iostream>
#include <sstream>
struct ABC {
int task(int until) {
for (int i = 0; i<10; ++i) {
if (i > until) {
std::ostringstream oss;
oss << "My exception happened in thread " << std::this_thread::get_id() << " at i=" << i;
throw std::runtime_error(oss.str());
}
}
return 42;
}
};
int main() {
for (int runs = 0; runs < 10; ++runs) {
ABC abc;
std::future<int> result = std::async(&ABC::task, &abc, rand()%20);
try {
std::cout << "Task returned " << result.get() << "
";
} catch (const std::exception &ex) {
std::cout << "Task exited with exception: " << ex.what() << "
";
std::cerr << "Thread exited with exception: " << ex.what() << "
";
}
}
}
王子:
Task returned Task exited with exception: My exception happened in thread 140288972076800 at i=4
Thread exited with exception: My exception happened in thread 140288972076800 at i=4
Task returned Task exited with exception: My exception happened in thread 140288972076800 at i=7
Thread exited with exception: My exception happened in thread 140288972076800 at i=7
Task returned 42
Task returned 42
Task returned 42
Task returned 42
Task returned Task exited with exception: My exception happened in thread 140288972076800 at i=7
Thread exited with exception: My exception happened in thread 140288972076800 at i=7
Task returned 42
Task returned 42
Task returned Task exited with exception: My exception happened in thread 140288972076800 at i=2
Thread exited with exception: My exception happened in thread 140288972076800 at i=2
另一答案
更新我的答案有缺陷且有错误,请参见sehe的评论。
我不确定你的最终目标是什么,但要弄清楚如何处理线程中的抛出。是的,您可以绕过编译器无法在具有Boost异常的线程之间抛出。
#include <boost/thread.hpp>
#include <boost/thread/scoped_thread.hpp>
#include <boost/chrono.hpp>
#include <iostream>
#include <boost/exception/all.hpp>
#include <exception>
boost::exception_ptr teptr;
class myexception: public std::exception
{
virtual const char* what() const throw()
{
return "My exception happened";
}
} myex;
class ABCc
{
public:
void start();
};
void ABCc::start()
{
int i=0;
std::cout<<"running the thread"<<std::endl;
while (1)
{
try
{
std::cout << "value of "<<i << '
';
if(i>2)
{
throw boost::enable_current_exception(myex);
}
i++;
}
catch (std::exception& e)
{
std::cout << "actual exception is"<<e.what() << '
';
teptr=boost::current_exception();
break;
// where were you going here???????
//throw myex;
}
}
}
int main()
{
ABCc abc;
boost::thread thread_point;
thread_point = boost::thread(&ABCc::start,abc);
while(1)
{
if (teptr) {
try {
boost::rethrow_exception(teptr);
}
catch(const std::exception &ex) {
std::cerr << "Thread may have exited; exception thrown: " << ex.what() << "
";
break;
}
}
}
std::cout << "exception should have been caught" << std::endl;
return 0;
}
请注意,您不必在main中进行throw / catch。你是通过在循环中使用boost :: thread来创建多个线程的,那是你的意图吗?
以上是关于从线程抛出异常并没有给出预期的结果的主要内容,如果未能解决你的问题,请参考以下文章