C++ (Qt)std::atomic<int>prev, cur;const int updateStep = 100; #pragma omp parallel forfor (int i = 0; i < vec.size(); ++i) { DoCalc(vec[i]); ++cur; #pragma omp master // обновляем индикатор в главной нитке через каждые 100 итераций { if (cur - prev >= updateStep) { UpdateIndicator(cur - prev); prev = cur; } }}
void foo(QFutureInterface<int> promise){ constexpr int iterations = 10000000; int result = 0; for (int i = 0; i < iterations; ++i) { if ((i % 1000) == 0 && promise.isPaused()) // relatively fast promise.waitForResume(); // slow, locks the mutex result += qHash(i); } promise.setResult(result);}
#include "runextensions.h"auto future = Utils::runAsync(foo);
#include "runextensions.h"auto future = Utils::runAsync(foo);future.pause();future.resume();future.waitForFinished();int result = future.result();qInfo() << result;
C++ (Qt)result += qHash(i);
C++ (Qt)if (promise.isCanceled()) return
C++ (Qt)void Widget::pause(int ms){ QEventLoop el; QTimer t; connect(&t, SIGNAL(timeout()), &el, SLOT(quit())); t.start(ms); el.exec();}