Inserita la QwaitCondition
authorEmmanuel Granatello <emmanuelgranatello@gmail.com>
Mon, 1 Mar 2010 14:34:25 +0000 (15:34 +0100)
committerEmmanuel Granatello <emmanuelgranatello@gmail.com>
Mon, 1 Mar 2010 14:34:25 +0000 (15:34 +0100)
Per risolvere alcuni problemi relativi al thread sono state agginte
le QMutex  e le QWaitCondition

Memory.pro
mainwindow.cpp
testThread.cpp
testThread.h

index dd0bef6..2746a86 100644 (file)
@@ -2,7 +2,7 @@
 # Project created by QtCreator 2010-02-18T12:16:41
 # -------------------------------------------------
 QT += svg
-TARGET = Memory
+TARGET = qmemory 
 TEMPLATE = app
 RESOURCES = qled.qrc
 SOURCES += main.cpp \
index 70fd57c..3bdfb6d 100644 (file)
@@ -9,8 +9,8 @@ MainWindow::MainWindow(QWidget *parent) :
     QMainWindow(parent),
     ui(new Ui::MainWindow)
 {
-    bestScore=0;
-    readSettings();
+            bestScore=0;
+            readSettings();
     ui->setupUi(this);
     statusLed = new QLed(ui->verticalLayoutWidget);
     ui->horizontalLayout_4->insertWidget(0,statusLed);
@@ -75,8 +75,11 @@ void MainWindow::sbagliato(int score){
     sscore.append("\n Best score: ");
     sscore.append(QString::number(bestScore));
     QMessageBox::warning(this, tr("End Game"),sscore,QMessageBox::Escape);
-    //tread.genera();
-    exit(0);
+    bestScore=0;
+    readSettings();
+    tread.genera();
+
+    //exit(0);
 
 }
 void MainWindow::disableAll(){
index fcdf34a..e3087e1 100644 (file)
@@ -5,7 +5,7 @@
 #include <QDateTime>
 TestThread::TestThread(QObject *parent):QThread(parent){
     colore=0;
-    attendiInputUtente=false;
+
     index=0;
     _score=0;
 
@@ -18,7 +18,7 @@ TestThread::~TestThread(){
 void TestThread::run(){
     sleep(1);
     while(true){
-
+    mutex.lock();
     qsrand(QDateTime::currentDateTime().toTime_t());
     sequenza.append(qrand()%9);
     int i=0;
@@ -35,12 +35,11 @@ void TestThread::run(){
       i++;
     }
     i=0;
-    attendiInputUtente=true;
     emit enableInterface();
+    attendiInputUtente.wait(&mutex);
+
+    mutex.unlock();
 
-    while(attendiInputUtente){
-        msleep(1);
-    }
     }
 
 
@@ -48,7 +47,7 @@ void TestThread::run(){
 
 void TestThread::updateCaption(int inputUser){
 
-    if(attendiInputUtente){
+
 
         if(sequenza.at(index)==inputUser)
         {
@@ -57,7 +56,7 @@ void TestThread::updateCaption(int inputUser){
             emit score(_score);
             if(index==sequenza.size()){
                 sleep(1);
-                attendiInputUtente=false;
+                attendiInputUtente.wakeAll();
                 index=0;
             }
         }
@@ -66,14 +65,14 @@ void TestThread::updateCaption(int inputUser){
             _score=0;
             sequenza.clear();
             index=0;
-            attendiInputUtente=true;
+            attendiInputUtente.wakeAll();
         }
-    }
+
 }
 
 void TestThread::genera()
 {
 
-    attendiInputUtente=false;
+    attendiInputUtente.wakeAll();
 
 }
index fdb6855..b5ad416 100644 (file)
@@ -2,6 +2,8 @@
 #define TESTTHREAD_H
 
 #include <QThread>
+#include <QWaitCondition>
+#include <QMutex>
 
 
 class TestThread : public QThread
@@ -26,10 +28,11 @@ public slots:
 
 
 private:
+    QWaitCondition attendiInputUtente;
     int colore;
     int index;
     int _score;
-    bool attendiInputUtente;
+    QMutex mutex;
     QList<int> sequenza;
 };
 #endif // TESTTHREAD_H