C++ 关于QtConcurrent::run中异常的通知

C++ 关于QtConcurrent::run中异常的通知,c++,multithreading,qt,exception,qt4,C++,Multithreading,Qt,Exception,Qt4,我知道实际处理在不同线程中抛出的异常是没有意义的,但是是否有某种方法可以通知我至少发生了异常?比如说 #include <QtConcurrentRun> #include <iostream> #include <stdexcept> void MyFunction() { // std::cout << "MyFunction()" << std::endl; throw std::runtime_error("Test

我知道实际处理在不同线程中抛出的异常是没有意义的,但是是否有某种方法可以通知我至少发生了异常?比如说

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

}
#包括
#包括
#包括
void MyFunction()
{

//std::coutQtConcurrent::run的好处在于它接受带有返回值的函数。 我的二分钱:尽早捕获异常

<>免责声明:我对C++中的异常非常不满意:d

在不同线程中调用的代码应返回一个值。对于void函数或未捕获异常的现有函数,您可以定义一个包装器(泛型或非泛型)。例如

 int exceptionwrapper(){
   int exception  = 0;
   try
   {
      myFunction();
   }
   catch(...){
      exception = 1;
      std::cout << "Exception caught!" << std::endl;
   }
   return exception;
 }
int exceptionwrapper(){
int异常=0;
尝试
{
myFunction();
}
捕获(…){
异常=1;

std::cout如果抛出异常,则关联QFutureWatcher的isCanceled()将返回true:

#include <QApplication>
#include <QtConcurrentRun>
#include <QFutureWatcher>

#include <iostream>
#include <stdexcept>

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

struct MyClass : public QObject
{
  Q_OBJECT

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

  void DoSomething()
  {
    QFuture<void> future = QtConcurrent::run(MyFunction);
    this->FutureWatcher.setFuture(future);
  }

  QFutureWatcher<void> FutureWatcher;

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

#include "Exception.moc"

int main(int argc, char*argv[])
{
  MyClass myClass;
  myClass.DoSomething();

  QApplication app(argc, argv);

  return app.exec();
}
#包括
#包括
#包括
#包括
#包括
void MyFunction()
{
std::cout FutureWatcher.setFuture(future);
}
QFutureWatcher未来观察者;
公众时段:
空槽_已完成()
{
标准::cout
我知道实际处理在不同线程中抛出的异常是没有意义的,但是是否有某种方法可以通知我至少发生了异常

您可以使用从
QtConcurrent::run
返回的future来处理它。有关详细信息,请参阅。在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();
}
#包括
#包括
#包括
类MyException:public QtConcurrent::Exception
{
公众:
MyException(std::exception&err):e(err){
void raise()常量{throw*this;}
异常*克隆()常量{返回新的MyException(*this);}
std::exception error()常量{return e;}
私人:
std::例外e;
};
//第一并发函数
地址五(地址n)
{
尝试
{
抛出std::runtime_错误(“kablammo!”);
//投掷-1;
返回n+5;
}
捕获(标准::异常&e)
{
抛出我的异常(e);
}
}
//第二并发函数
void myVoidFunction()
{
尝试
{
抛出std::runtime_错误(“oops!”);
//投掷-1;
}
捕获(标准::异常&e)
{
抛出我的异常(e);
}
}
int main(int argc,char*argv[])
{
QApplication应用程序(argc、argv);
QFuture f1=QtConcurrent::run(addFive,50);
尝试
{
int r=f1.result();

std::可能有问题。调用线程可能在异常发生之前就完成了受保护代码的执行。UmNyobe-那么QFuture呢?run需要qt4中的一个对象…UmNyobe-我明白你的意思。但是如果我有很多这样的函数,这可能会有相当高的成本似乎应该有一种更简单的方法来有效地将“抛出myException”转换为"cout-yes这是相当高的成本。如果你是一个模板主控程序,你可以用与
QtConcurrent::run
格式相同的模板来编写包装器,它总是返回一个包含异常类型和被调用方返回值的对象。或者你可以使用movetothread和signals以及slot…我已经添加了一个类似于ed的包装器的尝试这就是问题所在。显然我对函数指针做了一些错误的事情?结构看起来还好吗?(我把所有的东西都放在一个类中,这样FutureWatcher就可以持久,因为我们在这个演示中没有进入事件循环).你能看到我做错了什么吗?Intesting,谢谢!请查看上面对我答案的编辑-即使没有使用QtConcurrent::Exception,异常似乎也会被重新抛出?你能确认我在那里做的有意义吗(似乎有效)?@DavidDoria是的,这很有意义。Qt将记住并发线程中抛出的未处理异常,然后在将来收集时重新抛出它(使用
result()
waitForFinished()
,等等)如我的回答所示,使用包装器的唯一好处是,您可以检查调用线程中的异常(我想这就是您的问题所问的?)。当然,这需要您捕获它并将其存储在并发线程中。如果您实际上不关心异常的内容,那么您编辑的答案是完全正确的。
 QFuture<int> future = QtConcurrent::run(exemptionwrapper);
 futurewatcher.setFuture(future);
#include <QApplication>
#include <QtConcurrentRun>
#include <QFutureWatcher>

#include <iostream>
#include <stdexcept>

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

struct MyClass : public QObject
{
  Q_OBJECT

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

  void DoSomething()
  {
    QFuture<void> future = QtConcurrent::run(MyFunction);
    this->FutureWatcher.setFuture(future);
  }

  QFutureWatcher<void> FutureWatcher;

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

#include "Exception.moc"

int main(int argc, char*argv[])
{
  MyClass myClass;
  myClass.DoSomething();

  QApplication app(argc, argv);

  return app.exec();
}
#include <QtGui>
#include <iostream>
#include <stdexcept>

// non-void concurrent function
int addFive(int n)
{
  throw std::runtime_error("addFive throw!");
  return n+5;
}

// void concurrent function
void myVoidFunction()
{
   throw std::runtime_error("myVoidFunction throw!");
}

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 (...)
    {
      std::cout << "exception in addFive." << std::endl;
    }

    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 (...)
    {
       std::cout << "exception in myVoidFunction\n";
    }

    QWidget w;
    w.show();

    return app.exec();
}
#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();
}