
Originally Posted by
amleto
Your own example blocks with waitforfinished!!
At the end of my calculations on the main thread, I wait for the second thread to finish his calculations. In practice you could use two threads to do calculations that can be done in parallel. The main thread does his calculations while the second thread does his calculations as well, then the main thread waits for the second thread to finish working. The main thread then uses calculation results from both threads to make a final calculation.
Why would I ever want to do work on a second thread and then exit without waiting for the results?

Originally Posted by
amleto
Firstly, yes it IS in a separate thread. Secondly, where is that your requirement??
It seems kinda pointless to do work on a separate thread while the main thread can't carry out any calculations itself. The whole point of multithreading is that you can execute calculations in parallel.

Originally Posted by
amleto
In any event, if you want asych behaviour then just use map. like I said already.
If I change blockingMap to map in your code, the program exits before the exception is thrown. That isn't very productive. This seems to work though:
#include <QString>
#include <QtConcurrentRun>
#include <iostream>
#include <vector>
#include <QtCore>
using std::cerr;
using std::ostream;
class MyException : public QtConcurrent::Exception {
public:
MyException(const QString& msg) { _msg = msg; }
virtual ~MyException() throw() {}
const QString& msg() const { return _msg; }
void raise() const { throw *this; }
Exception *clone() const { return new MyException(*this); }
private:
};
void separateThread(int) {
int i = 0;
while (i < 10000)
++i;
throw MyException("This exception should be caught by the main thread");
}
int main()
{
std::vector<int> ints(1);
QFuture<void> future;
try
{
future = QtConcurrent::map(ints, separateThread);
int i = 0;
while (i < 10000)
++i;
future.waitForFinished();
}
catch (const MyException& e)
{
std::cerr << "QUITTING WITH ERROR\n" << e.msg().toStdString() << std::endl;
}
return 0;
}
#include <QString>
#include <QtConcurrentRun>
#include <iostream>
#include <vector>
#include <QtCore>
using std::cerr;
using std::ostream;
class MyException : public QtConcurrent::Exception {
public:
MyException(const QString& msg) { _msg = msg; }
virtual ~MyException() throw() {}
const QString& msg() const { return _msg; }
void raise() const { throw *this; }
Exception *clone() const { return new MyException(*this); }
private:
QString _msg;
};
void separateThread(int) {
int i = 0;
while (i < 10000)
++i;
throw MyException("This exception should be caught by the main thread");
}
int main()
{
std::vector<int> ints(1);
QFuture<void> future;
try
{
future = QtConcurrent::map(ints, separateThread);
int i = 0;
while (i < 10000)
++i;
future.waitForFinished();
}
catch (const MyException& e)
{
std::cerr << "QUITTING WITH ERROR\n" << e.msg().toStdString() << std::endl;
}
return 0;
}
To copy to clipboard, switch view to plain text mode
Bookmarks