From 0748751c9f097d9d866605306bbdd4b96e2a5c4e Mon Sep 17 00:00:00 2001 From: David Boddie Date: Wed, 27 Apr 2011 19:16:41 +0200 Subject: Squashed commit of the changes from the mobile-examples repository (4.7-generated-declarative branch). --- examples/threads/waitconditions/waitconditions.cpp | 132 +++++++++++++++------ 1 file changed, 95 insertions(+), 37 deletions(-) (limited to 'examples/threads/waitconditions/waitconditions.cpp') diff --git a/examples/threads/waitconditions/waitconditions.cpp b/examples/threads/waitconditions/waitconditions.cpp index 5c8cb7149f..0063db5b91 100644 --- a/examples/threads/waitconditions/waitconditions.cpp +++ b/examples/threads/waitconditions/waitconditions.cpp @@ -38,13 +38,18 @@ ** ****************************************************************************/ -#include +#include #include #include //! [0] +#ifdef Q_WS_S60 +const int DataSize = 300; +#else const int DataSize = 100000; +#endif + const int BufferSize = 8192; char buffer[BufferSize]; @@ -59,60 +64,110 @@ class Producer : public QThread //! [1] //! [2] { public: - void run(); -}; + Producer(QObject *parent = NULL) : QThread(parent) + { + } -void Producer::run() -{ - qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); - - for (int i = 0; i < DataSize; ++i) { - mutex.lock(); - if (numUsedBytes == BufferSize) - bufferNotFull.wait(&mutex); - mutex.unlock(); - - buffer[i % BufferSize] = "ACGT"[(int)qrand() % 4]; - - mutex.lock(); - ++numUsedBytes; - bufferNotEmpty.wakeAll(); - mutex.unlock(); + void run() + { + qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); + + for (int i = 0; i < DataSize; ++i) { + mutex.lock(); + if (numUsedBytes == BufferSize) + bufferNotFull.wait(&mutex); + mutex.unlock(); + + buffer[i % BufferSize] = "ACGT"[(int)qrand() % 4]; + + mutex.lock(); + ++numUsedBytes; + bufferNotEmpty.wakeAll(); + mutex.unlock(); + } } -} +}; //! [2] //! [3] class Consumer : public QThread //! [3] //! [4] { + Q_OBJECT public: - void run(); + Consumer(QObject *parent = NULL) : QThread(parent) + { + } + + void run() + { + for (int i = 0; i < DataSize; ++i) { + mutex.lock(); + if (numUsedBytes == 0) + bufferNotEmpty.wait(&mutex); + mutex.unlock(); + + #ifdef Q_WS_S60 + emit stringConsumed(QString(buffer[i % BufferSize])); + #else + fprintf(stderr, "%c", buffer[i % BufferSize]); + #endif + + mutex.lock(); + --numUsedBytes; + bufferNotFull.wakeAll(); + mutex.unlock(); + } + fprintf(stderr, "\n"); + } + +signals: + void stringConsumed(const QString &text); }; +//! [4] -void Consumer::run() +#ifdef Q_WS_S60 +class PlainTextEdit : public QPlainTextEdit { - for (int i = 0; i < DataSize; ++i) { - mutex.lock(); - if (numUsedBytes == 0) - bufferNotEmpty.wait(&mutex); - mutex.unlock(); - - fprintf(stderr, "%c", buffer[i % BufferSize]); - - mutex.lock(); - --numUsedBytes; - bufferNotFull.wakeAll(); - mutex.unlock(); + Q_OBJECT +public: + PlainTextEdit(QWidget *parent = NULL) : QPlainTextEdit(parent), producer(NULL), consumer(NULL) + { + setTextInteractionFlags(Qt::NoTextInteraction); + + producer = new Producer(this); + consumer = new Consumer(this); + + QObject::connect(consumer, SIGNAL(stringConsumed(const QString &)), SLOT(insertPlainText(const QString &)), Qt::BlockingQueuedConnection); + + QTimer::singleShot(0, this, SLOT(startThreads())); } - fprintf(stderr, "\n"); -} -//! [4] + +protected: + Producer *producer; + Consumer *consumer; + +protected slots: + void startThreads() + { + producer->start(); + consumer->start(); + } +}; +#endif //! [5] int main(int argc, char *argv[]) //! [5] //! [6] { +#ifdef Q_WS_S60 + QApplication app(argc, argv); + + PlainTextEdit console; + console.showMaximized(); + + return app.exec(); +#else QCoreApplication app(argc, argv); Producer producer; Consumer consumer; @@ -121,5 +176,8 @@ int main(int argc, char *argv[]) producer.wait(); consumer.wait(); return 0; +#endif } //! [6] + +#include "waitconditions.moc" -- cgit v1.2.3