Close old file and open new one
[agianapa.git] / qt / sphere-orbit-camera-gui / workerthread.cpp
blobbd99b4238dd0544093e9c8faca64d0d6da7d67b7
1 #include <cstdlib>
2 #include <iostream>
4 #include "workerthread.h"
6 WorkerThread::WorkerThread(QObject *parent, QQueue<float> *pData)
7 : QThread(parent)
9 // Initialize random generator here
10 srand(time(NULL));
12 m_file.setFileName("foo.dat");
13 m_file.open(QIODevice::ReadOnly);
14 in.setDevice(&m_file);
16 abort = false;
17 prev = 0;
18 this->pData = pData;
21 WorkerThread::~WorkerThread()
23 std::cout << "WorkerThread::~WorkerThread()\n";
25 mutex.lock();
26 abort = true;
27 condition.wakeOne();
28 mutex.unlock();
30 m_file.close();
32 wait();
35 void WorkerThread::run(void)
37 float r;
39 // Loop
40 while (!abort) {
41 mutex.lock();
42 if (pData->size() >= 667) {
43 std::cout << "WorkerThread::run()\t[BLOCKED]\n";
44 condition.wait(&mutex);
45 mutex.unlock();
47 else {
48 in >> r;
49 pData->enqueue(r);
50 mutex.unlock();
55 void WorkerThread::setData(QQueue<float> *pData)
57 this->pData = pData;
60 void WorkerThread::setFileName(QString fileName)
62 std::cout << "WorkerThread::setFileName()\n";
64 // Close old file
65 m_file.close();
67 // Open new one
68 m_file.setFileName(fileName);
69 m_file.open(QIODevice::ReadOnly);
70 in.setDevice(&m_file);