Hi, I'm learning QMutex class and I have some problems. This is the code I have so far:
test.h
#ifndef TEST_H
#define TEST_H
#include <QtGui>
#include <iostream>
#include <QThread>
Q_OBJECT
public:
void setMessage1
(const QString &message
);
void setMessage2
(const QString &message
);
void printMessage
(const QString &t
);
void stop();
protected:
void run();
private:
};
class ThreadDialog
: public QDialog { Q_OBJECT
public:
};
#endif
#ifndef TEST_H
#define TEST_H
#include <QtGui>
#include <iostream>
#include <QThread>
class Thread : public QThread {
Q_OBJECT
public:
Thread(const QString &mes);
void setMessage1(const QString &message);
void setMessage2(const QString &message);
void printMessage(const QString &t);
void stop();
protected:
void run();
private:
QString message_str;
QMutex mutex;
};
class ThreadDialog : public QDialog {
Q_OBJECT
public:
ThreadDialog(QWidget *parent = 0);
};
#endif
To copy to clipboard, switch view to plain text mode
test.cpp
#ifndef TEST_CPP
#define TEST_CPP
#include "test.h"
Thread
::Thread(const QString &mes
) { message_str = mes;
}
void Thread::run() {
//mutex.lock();
//QThread::msleep(1000);
//printMessage("START");
//mutex.unlock();
}
void Thread::stop() {
//mutex.lock();
//printMessage("STOP");
//mutex.unlock();
}
void Thread
::setMessage1(const QString &message
) { //mutex.lock();
printMessage("START");
message_str = message;
printMessage("STOP");
//mutex.unlock();
}
void Thread
::setMessage2(const QString &message
) { //mutex.lock();
printMessage("START");
message_str = message;
printMessage("STOP");
//mutex.unlock();
}
void Thread
::printMessage(const QString &t
) { qDebug() << t << ": " << message_str << flush;
}
Thread threadA("A");
Thread threadB("B");
threadA.start();
threadB.start();
threadA.setMessage1("A");
threadB.setMessage2("B");
//threadB.stop();
//threadA.stop();
}
#endif
#ifndef TEST_CPP
#define TEST_CPP
#include "test.h"
Thread::Thread(const QString &mes) {
message_str = mes;
}
void Thread::run() {
//mutex.lock();
//QThread::msleep(1000);
//printMessage("START");
//mutex.unlock();
}
void Thread::stop() {
//mutex.lock();
//printMessage("STOP");
//mutex.unlock();
}
void Thread::setMessage1(const QString &message) {
//mutex.lock();
printMessage("START");
message_str = message;
QThread::msleep(2000);
printMessage("STOP");
//mutex.unlock();
}
void Thread::setMessage2(const QString &message) {
//mutex.lock();
printMessage("START");
message_str = message;
QThread::msleep(2000);
printMessage("STOP");
//mutex.unlock();
}
void Thread::printMessage(const QString &t) {
qDebug() << t << ": " << message_str << flush;
}
ThreadDialog::ThreadDialog(QWidget *parent) : QDialog(parent) {
Thread threadA("A");
Thread threadB("B");
threadA.start();
threadB.start();
threadA.setMessage1("A");
threadB.setMessage2("B");
//threadB.stop();
//threadA.stop();
}
#endif
To copy to clipboard, switch view to plain text mode
main.cpp
#include <QApplication>
#include "test.h"
int main(int argc, char **argv) {
ThreadDialog *dialog = new ThreadDialog;
//dialog->show();
return app.exec();
}
#include <QApplication>
#include "test.h"
int main(int argc, char **argv) {
QApplication app(argc, argv);
ThreadDialog *dialog = new ThreadDialog;
//dialog->show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
I wish to change the program so that both threads would be started first and then stopped...but when used with QMutex the output would be first one is stared&stoped and then the second started&stopped...now the second scenario already happens, but it shouldn't, why is that?
Bookmarks