c++ - QtConcurrent::run 异常通知

标签 c++ multithreading qt exception qt4

我知道实际处理在不同线程中抛出的异常没有意义,但是有什么方法可以通知我至少发生了异常吗?例如。类似

#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;
  }

}

即使发生异常,也会安静地退出。当异常来自调用堆栈深处的某处时,这有时会让人非常困惑。

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

我试过像 UmNyobe 建议的那样编写一个包装器,但我一定是在函数指针方面做错了什么?

#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();
}

我得到的错误是在这一行:

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 来处理它。参见 this page了解详情。当您收集 future 时,将重新抛出任何未处理的异常。您可以制作一个简单的包装类来捕获异常并在接收线程中检查它。

#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();
}

关于c++ - QtConcurrent::run 异常通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13727506/

相关文章:

c++ - 使用 QStyle 设置全局默认最小 QPushButton 高度

java - 如何使用自己的线程池和其他配置独立运行两个进程?

c++ - 多线程函数重启模式/解决方案

c++ - 使用 boost Spirit x3 解析为集合

c++ - 为什么错误: ‘locate_zone’ is not a member of ‘std::chrono’

linux - 修改内核调度程序,为特定程序提供更长的时间片

c++ - Qt creator 中的 Pantheios 与 VS C++ 2008 编译器

database - 是否可以只使用一个 Qt 模块,就像第 3 方 dll 一样?

c++ - 如果队列为空,dequeue 应该返回什么

c++ - 调试链接错误的最佳实践