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.

Qt Code:
  1. #include <QtConcurrent>
  2. #include <QThreadPool>
  3.  
  4. #include <vector>
  5.  
  6. int main(int argc, char ** argv)
  7. {
  8. std::vector<int> ones(100, 1);
  9. ones[10] = 123; // special value
  10.  
  11. QThreadPool::globalInstance()->setMaxThreadCount(1);
  12.  
  13. auto times_two = [](int & i)
  14. {
  15. if (i == 123) // when reaching the special value, I want to stop the other jobs here
  16. QThreadPool::globalInstance()->clear();
  17. else
  18. i *= 2;
  19. };
  20.  
  21. QtConcurrent::blockingMap(ones.begin(), ones.end(), times_two);
  22.  
  23. return 0;
  24. }
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.