diff options
author | David Boddie <david.boddie@nokia.com> | 2011-04-28 17:39:11 (GMT) |
---|---|---|
committer | David Boddie <david.boddie@nokia.com> | 2011-04-28 17:39:11 (GMT) |
commit | 3abaecc3aec4e46f1c5969c33875fd45aa542385 (patch) | |
tree | 58613c5d2b765018c31d0b189e9c5a34d8adde69 /examples/threads/waitconditions | |
parent | ddb22795641253a026b72f752ebc769745dd41be (diff) | |
download | Qt-3abaecc3aec4e46f1c5969c33875fd45aa542385.zip Qt-3abaecc3aec4e46f1c5969c33875fd45aa542385.tar.gz Qt-3abaecc3aec4e46f1c5969c33875fd45aa542385.tar.bz2 |
Squashed commit of the changes from the mobile-examples repository
(4.7-generated-declarative branch).
Diffstat (limited to 'examples/threads/waitconditions')
-rw-r--r-- | examples/threads/waitconditions/waitconditions.cpp | 132 | ||||
-rw-r--r-- | examples/threads/waitconditions/waitconditions.desktop | 11 | ||||
-rw-r--r-- | examples/threads/waitconditions/waitconditions.pro | 5 |
3 files changed, 110 insertions, 38 deletions
diff --git a/examples/threads/waitconditions/waitconditions.cpp b/examples/threads/waitconditions/waitconditions.cpp index 5c8cb71..0063db5 100644 --- a/examples/threads/waitconditions/waitconditions.cpp +++ b/examples/threads/waitconditions/waitconditions.cpp @@ -38,13 +38,18 @@ ** ****************************************************************************/ -#include <QtCore> +#include <QtGui> #include <stdio.h> #include <stdlib.h> //! [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" diff --git a/examples/threads/waitconditions/waitconditions.desktop b/examples/threads/waitconditions/waitconditions.desktop new file mode 100644 index 0000000..4fbc304 --- /dev/null +++ b/examples/threads/waitconditions/waitconditions.desktop @@ -0,0 +1,11 @@ +[Desktop Entry] +Encoding=UTF-8 +Version=1.0 +Type=Application +Terminal=false +Name=Wait Conditions +Exec=/opt/usr/bin/waitconditions +Icon=waitconditions +X-Window-Icon= +X-HildonDesk-ShowInToolbar=true +X-Osso-Type=application/x-executable diff --git a/examples/threads/waitconditions/waitconditions.pro b/examples/threads/waitconditions/waitconditions.pro index 4f5d1d4..5329286 100644 --- a/examples/threads/waitconditions/waitconditions.pro +++ b/examples/threads/waitconditions/waitconditions.pro @@ -3,8 +3,8 @@ ###################################################################### TEMPLATE = app +QT = core gui CONFIG -= moc app_bundle -QT -= gui DEPENDPATH += . INCLUDEPATH += . @@ -19,3 +19,6 @@ sources.path = $$[QT_INSTALL_EXAMPLES]/threads/waitconditions INSTALLS += target sources symbian: include($$QT_SOURCE_TREE/examples/symbianpkgrules.pri) +maemo5: include($$QT_SOURCE_TREE/examples/maemo5pkgrules.pri) + +simulator: warning(This example might not fully work on Simulator platform) |