55 lines
1.4 KiB
C++
55 lines
1.4 KiB
C++
#include "dataqueue.h"
|
|
|
|
|
|
DataQueue::DataQueue(QObject *parent) :
|
|
QThread(parent), stopped(false)
|
|
{}
|
|
|
|
|
|
DataQueue::~DataQueue(){
|
|
}
|
|
|
|
void DataQueue::run() {
|
|
mutex.lock();
|
|
bool stop = stopped || (requestQueue.size() == 0);
|
|
mutex.unlock();
|
|
while (!stop) {
|
|
mutex.lock();
|
|
if (requestQueue.size() > 0) {
|
|
DataResult1D data = loader->doLoad(requestQueue.front());
|
|
resultMap.insert(std::pair<std::string, DataResult1D>(data.id, data));
|
|
emit resultReady(QString::fromStdString(data.id));
|
|
requestQueue.pop();
|
|
stop = requestQueue.size() == 0 || stopped;
|
|
}
|
|
mutex.unlock();
|
|
}
|
|
}
|
|
|
|
void DataQueue::requestData(const nix::DataArray &array, const nix::NDSize &count, const nix::NDSize &offset, std::string &requestId) {
|
|
requestId = nix::util::createId();
|
|
DataRequest dr(requestId, array, count, offset);
|
|
mutex.lock();
|
|
requestQueue.push(dr);
|
|
mutex.unlock();
|
|
start();
|
|
}
|
|
|
|
void DataQueue::stop() {
|
|
mutex.lock();
|
|
stopped = true;
|
|
mutex.unlock();
|
|
}
|
|
|
|
DataResult1D DataQueue::getData(QString requestId) {
|
|
DataResult1D ret;
|
|
mutex.lock();
|
|
std::map<std::string, DataResult1D>::iterator it = resultMap.find(requestId.toStdString());
|
|
if (it != resultMap.end()) {
|
|
ret = it->second;
|
|
resultMap.erase(it);
|
|
}
|
|
mutex.unlock();
|
|
return ret;
|
|
}
|