Hello,
I am looking for a way to interrupt a QtConcurrent::blockingMap's job. I tried calling clear() on the QThreadPool's global instance, but it doesn't work as I expected. Here is a minimal example showing what I'm trying to do.
#include <QtConcurrent>
#include <QThreadPool>
#include <vector>
int main(int argc, char ** argv)
{
std::vector<int> ones(100, 1);
ones[10] = 123; // special value
QThreadPool::globalInstance()->setMaxThreadCount(1);
auto times_two = [](int & i)
{
if (i == 123) // when reaching the special value, I want to stop the other jobs here
QThreadPool::globalInstance()->clear();
else
i *= 2;
};
QtConcurrent::blockingMap(ones.begin(), ones.end(), times_two);
return 0;
}
#include <QtConcurrent>
#include <QThreadPool>
#include <vector>
int main(int argc, char ** argv)
{
std::vector<int> ones(100, 1);
ones[10] = 123; // special value
QThreadPool::globalInstance()->setMaxThreadCount(1);
auto times_two = [](int & i)
{
if (i == 123) // when reaching the special value, I want to stop the other jobs here
QThreadPool::globalInstance()->clear();
else
i *= 2;
};
QtConcurrent::blockingMap(ones.begin(), ones.end(), times_two);
return 0;
}
To copy to clipboard, switch view to plain text mode
Here, each value inside a vector is multiplied by 2, until a special value (123) is found. Then, the call to clear() seems to have no effect whatsoever, i.e. the lambda "times_two" is called on the rest of the vector. But according to the documentation, QThreadPool::clear() is supposed to "remove the runnables that are not yet started from the queue".
Is there something I am doing wrong here ?
Thanks.
Bookmarks