Risolti alcuni bug del thread
[qmemory] / testThread.cpp
1 #include "testThread.h"
2 #include <QTimer>
3 #include <qdebug.h>
4 #include <QMessageBox>
5 #include <QDateTime>
6
7 TestThread::TestThread(QObject *parent):QThread(parent)
8 {
9     colore=0;
10     index=0;
11     _score=0;
12     qsrand(QDateTime::currentDateTime().toTime_t());
13
14 }
15
16 TestThread::~TestThread()
17 {
18
19 }
20 void TestThread::run(){
21     sleep(1);
22     forever{
23         mutex.lock();
24         qsrand(QDateTime::currentDateTime().toTime_t());
25         sequenza.append(qrand()%9);
26         int i=0;
27         index=0;
28         emit disableInterface();
29         while(i<sequenza.size())
30         {
31           emit generate(sequenza.at(i));
32           msleep(500 - (_score*2));
33           emit generate(sequenza.at(i));
34           msleep(500 - (_score*2));
35           i++;
36         }
37
38         i=0;
39         emit enableInterface();
40         attendiInputUtente.wait(&mutex);
41         mutex.unlock();
42     }
43 }
44
45 void TestThread::addUserInput(int inputUser){
46         if(sequenza.at(index)==inputUser)
47         {
48             index++;
49             _score++;
50             emit score(_score);
51             if(index==sequenza.size()){
52                 sleep(1);
53                 attendiInputUtente.wakeAll();
54             }
55         }
56         else{
57             emit exit(_score);
58         }
59
60 }
61
62 void TestThread::genera()
63 {
64     sequenza.clear();
65     index=0;
66     _score=0;
67     emit score(_score);
68     attendiInputUtente.wakeAll();
69 }