Risolti alcuni bug del thread
[qmemory] / testThread.cpp
index 7c6ba3c..84915c3 100644 (file)
@@ -3,80 +3,67 @@
 #include <qdebug.h>
 #include <QMessageBox>
 #include <QDateTime>
-TestThread::TestThread(QObject *parent):QThread(parent){
-    colore=0;
-    attendiInputUtente=false;
 
-    _score=0;
+TestThread::TestThread(QObject *parent):QThread(parent)
+{
+    colore=0;
     index=0;
-
+    _score=0;
     qsrand(QDateTime::currentDateTime().toTime_t());
 
 }
-TestThread::~TestThread(){
 
-}
-void TestThread::run()
+TestThread::~TestThread()
 {
-    while(true){
-        //msleep(200);
+
+}
+void TestThread::run(){
+    sleep(1);
+    forever{
+        mutex.lock();
         qsrand(QDateTime::currentDateTime().toTime_t());
         sequenza.append(qrand()%9);
         int i=0;
-
-        qDebug()<<"Init Emission";
+        index=0;
         emit disableInterface();
         while(i<sequenza.size())
         {
-          qDebug()<<"Init Generate";
           emit generate(sequenza.at(i));
-          qDebug()<<sequenza.at(i);
-          msleep(500 - (_score*5));
+          msleep(500 - (_score*2));
           emit generate(sequenza.at(i));
-          msleep(500 - (_score*5));
+          msleep(500 - (_score*2));
           i++;
         }
+
         i=0;
-        attendiInputUtente=true;
         emit enableInterface();
-
-        while(attendiInputUtente){
-            msleep(1);
-        } ;
+        attendiInputUtente.wait(&mutex);
+        mutex.unlock();
     }
-
-
 }
 
-void TestThread::updateCaption(int inputUser)
-{
-     qDebug()<<"Entro in update Caption ="<< inputUser<< " valore aspettato" << sequenza.at(index);
-     if (attendiInputUtente){
-
-        if(sequenza.at(index) == inputUser)
+void TestThread::addUserInput(int inputUser){
+        if(sequenza.at(index)==inputUser)
         {
-            if(index == sequenza.size() - 1){
-                qDebug()<<"Entro in if if ="<< inputUser<< " valore aspettato" << sequenza.at(index);
-                attendiInputUtente=false;
-                index=0;
-            }else
-            {
-                index++;
-                _score++;
+            index++;
+            _score++;
+            emit score(_score);
+            if(index==sequenza.size()){
+                sleep(1);
+                attendiInputUtente.wakeAll();
             }
         }
         else{
-            emit score(_score);
-            _score=0;
-            sequenza.clear();
-            index=0;
-            attendiInputUtente=true;
+            emit exit(_score);
         }
-    }
+
 }
 
 void TestThread::genera()
 {
-    attendiInputUtente=false;
-
+    sequenza.clear();
+    index=0;
+    _score=0;
+    emit score(_score);
+    attendiInputUtente.wakeAll();
 }