#include <QCoreApplication>
#include <QTimer>
#include <QThread>
#include <QMutex>
#include <QMutexLocker>
#include <QDebug>
struct Data {
Data
(): mutex
(new QMutex), value
(0) { } ~Data() { delete mutex; }
int value;
};
{
Q_OBJECT
Data *d;
public:
Writer
(Data
*data,
QObject *p
= 0): d(data)
{ }
signals:
void finished();
public slots:
void process() {
connect(timer, SIGNAL(timeout()), SLOT(tick()));
timer->setInterval(1000);
timer->start();
QTimer::singleShot(8000,
this,
SIGNAL(finished
()));
// self-destruct in 8 secs }
void tick() {
qDebug() << "Try writing";
d->value++;
qDebug() << "Written" << d->value;
}
};
{
Q_OBJECT
int n;
Data *d;
public:
Reader
(int number, Data
*data,
QObject *p
= 0): n(number),
d(data)
{ }
signals:
void finished();
public slots:
void process() {
connect(timer, SIGNAL(timeout()), SLOT(tick()));
timer->setInterval(1000);
timer->start();
QTimer::singleShot(10000,
this,
SIGNAL(finished
()));
// self-destruct in 10 secs }
void tick() {
qDebug() << n << "Try reading";
qDebug() << n << "Read" << d->value;
}
};
int main(int argc, char **argv)
{
Data data; // the shared data
Writer* writer = new Writer(&data);
writer->moveToThread(thread);
QObject::connect(thread,
SIGNAL(started
()), writer,
SLOT(process
()));
QObject::connect(writer,
SIGNAL(finished
()), thread,
SLOT(quit
()));
QObject::connect(writer,
SIGNAL(finished
()), writer,
SLOT(deleteLater
()));
QObject::connect(thread,
SIGNAL(finished
()), thread,
SLOT(deleteLater
()));
thread->start();
for (int n = 0; n < 3; ++n) {
Reader* reader = new Reader(n, &data);
reader->moveToThread(thread);
QObject::connect(thread,
SIGNAL(started
()), reader,
SLOT(process
()));
QObject::connect(reader,
SIGNAL(finished
()), thread,
SLOT(quit
()));
QObject::connect(reader,
SIGNAL(finished
()), reader,
SLOT(deleteLater
()));
QObject::connect(thread,
SIGNAL(finished
()), thread,
SLOT(deleteLater
()));
thread->start();
}
return app.exec();
}
#include "main.moc"
#include <QCoreApplication>
#include <QTimer>
#include <QThread>
#include <QMutex>
#include <QMutexLocker>
#include <QDebug>
struct Data {
Data(): mutex(new QMutex), value(0) { }
~Data() { delete mutex; }
QMutex *mutex;
int value;
};
class Writer: public QObject
{
Q_OBJECT
Data *d;
public:
Writer(Data *data, QObject *p = 0):
QObject(p),
d(data)
{ }
signals:
void finished();
public slots:
void process() {
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), SLOT(tick()));
timer->setInterval(1000);
timer->start();
QTimer::singleShot(8000, this, SIGNAL(finished())); // self-destruct in 8 secs
}
void tick() {
qDebug() << "Try writing";
QMutexLocker locker(d->mutex);
d->value++;
qDebug() << "Written" << d->value;
}
};
class Reader: public QObject
{
Q_OBJECT
int n;
Data *d;
public:
Reader(int number, Data *data, QObject *p = 0):
QObject(p),
n(number),
d(data)
{ }
signals:
void finished();
public slots:
void process() {
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), SLOT(tick()));
timer->setInterval(1000);
timer->start();
QTimer::singleShot(10000, this, SIGNAL(finished())); // self-destruct in 10 secs
}
void tick() {
qDebug() << n << "Try reading";
QMutexLocker locker(d->mutex);
qDebug() << n << "Read" << d->value;
}
};
int main(int argc, char **argv)
{
QCoreApplication app(argc, argv);
Data data; // the shared data
QThread* thread = new QThread(qApp);
Writer* writer = new Writer(&data);
writer->moveToThread(thread);
QObject::connect(thread, SIGNAL(started()), writer, SLOT(process()));
QObject::connect(writer, SIGNAL(finished()), thread, SLOT(quit()));
QObject::connect(writer, SIGNAL(finished()), writer, SLOT(deleteLater()));
QObject::connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
thread->start();
for (int n = 0; n < 3; ++n) {
thread = new QThread(qApp);
Reader* reader = new Reader(n, &data);
reader->moveToThread(thread);
QObject::connect(thread, SIGNAL(started()), reader, SLOT(process()));
QObject::connect(reader, SIGNAL(finished()), thread, SLOT(quit()));
QObject::connect(reader, SIGNAL(finished()), reader, SLOT(deleteLater()));
QObject::connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
thread->start();
}
return app.exec();
}
#include "main.moc"
To copy to clipboard, switch view to plain text mode
Bookmarks