有关QtConcurrent :: run中的异常的通知 [英] Notification about exceptions in QtConcurrent::run

查看:807
本文介绍了有关QtConcurrent :: run中的异常的通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道实际上不处理在另一个线程中引发的异常是没有意义的,但是有什么方法可以通知我至少发生了异常?例如.像

I know it doesn't make sense to actually handle an exception thrown in a different thread, but is there some way I can get notified that at least an exception occurred? E.g. something like

#include <QtConcurrentRun>

#include <iostream>
#include <stdexcept>

void MyFunction()
{
//  std::cout << "MyFunction()" << std::endl;
  throw std::runtime_error("Test exception.");
}

int main()
{
  try
  {
    QtConcurrent::run(MyFunction);
  }
  catch(...)
  {
    std::cout << "Exception caught!" << std::endl;
  }

}

即使发生异常,

也会安静地退出.当异常来自某个调用堆栈深处的某个地方时,这有时会造成混乱.

exits quietly, even though an exception occurred. This is sometimes very confusing when the exception comes from deep down in the call stack somewhere.

------------编辑-------------

------------EDIT-------------

我试图写一个像UmNyobe建议的包装器,但是我在函数指针方面一定做错了吗?

I tried to write a wrapper like UmNyobe suggested, but I must be doing something wrong with the function pointers?

#include <QtConcurrentRun>
#include <QFutureWatcher>
#include <QObject>

#include <iostream>
#include <stdexcept>

void MyFunction()
{
//  std::cout << "MyFunction()" << std::endl;
  throw std::runtime_error("Test exception.");
}

template<typename TFirstParam, typename... TParams>
bool ExceptionWrapper(TFirstParam firstParam, TParams&& ...params)
{
  // Here 'firstParam' should be a function pointer, and 'params' are the arguments
  // that should be passed to the function
  try
  {
    firstParam(params...);
  }
  catch(...)
  {
    std::cout << "Exception caught!" << std::endl;
    return false; // failure
  }

  return true; // success
}

struct MyClass : public QObject
{
  Q_OBJECT

  MyClass()
  {
    connect(&this->FutureWatcher, SIGNAL(finished()), this, SLOT(slot_finished()));
  }

  void DoSomething()
  {
    void (*myFunctionPointer)() = MyFunction;
    bool (*functionPointer)(decltype(myFunctionPointer)) = ExceptionWrapper;

    QFuture<bool> future = QtConcurrent::run(functionPointer);
    this->FutureWatcher.setFuture(future);
  }

  QFutureWatcher<void> FutureWatcher;

  void slot_finished()
  {
    std::cout << "Finished" << std::endl;
    if(!this->FutureWatcher.result())
    {
      std::cout << "There was an error!" << std::endl;
    }
  }
};

#include "ExceptionWrapper.moc"

int main()
{
  MyClass myClass = new MyClass;
  myClass->DoSomething();
}

我得到的错误是在这条线上:

The error I get is on this line:

QFuture<bool> future = QtConcurrent::run(functionPointer);

error: no matching function for call to 'run(bool (*&)(void (*)()))'

推荐答案

我知道实际上不处理在另一个线程中引发的异常是没有意义的,但是有什么方法可以通知我至少发生了异常?

I know it doesn't make sense to actually handle an exception thrown in a different thread, but is there some way I can get notified that at least an exception occurred?

您可以使用QtConcurrent::run返回的Future来处理它.有关详细信息,请参见本页.当您将来收集时,所有未处理的异常都将被重新抛出.您可以创建一个简单的包装器类来捕获异常并在接收线程中对其进行检查.

You can handle it by using the future returned from QtConcurrent::run. See this page for details. When you collect on the future, any unhandled exceptions will be rethrown. You can make a simple wrapper class to capture an exception and examine it in the receiving thread.

#include <QtGui>
#include <iostream>
#include <stdexcept>

class MyException : public QtConcurrent::Exception
{
public:
    MyException(std::exception& err) : e(err) {}
    void raise() const { throw *this; }
    Exception* clone() const { return new MyException(*this); }
    std::exception error() const { return e; }
private:
    std::exception e;
};

// first concurrent function
int addFive(int n)
{
    try
    {
        throw std::runtime_error("kablammo!");
        //throw -1;
        return n + 5;
    }
    catch (std::exception& e)
    {
        throw MyException(e);
    }

}

// second concurrent function    
void myVoidFunction()
{
    try
    {
        throw std::runtime_error("oops!");
        //throw -1;
    }
    catch (std::exception& e)
    {
        throw MyException(e);
    }
}

int main(int argc, char* argv[])
{
    QApplication app(argc, argv);

    QFuture<int> f1 = QtConcurrent::run(addFive, 50);
    try
    {
        int r = f1.result();
        std::cout << "result = " << r << std::endl;
    }
    catch (MyException& me)
    {
        std::cout << me.error().what() << std::endl;
    }
    catch (QtConcurrent::UnhandledException&)
    {
        std::cout << "unhandled exception in addFive\n";
    }

    QFuture<void> f2 = QtConcurrent::run(myVoidFunction);
    try
    {
        // result() not available for QFuture<void>, use waitForFinished() to
        // block until it's done.
        f2.waitForFinished();
        std::cout << "myVoidFunction finished\n";
    }
    catch (MyException& me)
    {
        std::cout << me.error().what() << std::endl;
    }
    catch (QtConcurrent::UnhandledException&)
    {
        std::cout << "unhandled exception in myVoidFunction\n";
    }

    QWidget w;
    w.show();

    return app.exec();
}

这篇关于有关QtConcurrent :: run中的异常的通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆