summaryrefslogtreecommitdiffstats
path: root/examples/threads/waitconditions
diff options
context:
space:
mode:
authorDavid Boddie <david.boddie@nokia.com>2011-04-27 19:16:41 +0200
committerDavid Boddie <david.boddie@nokia.com>2011-05-23 14:24:07 +0200
commit0748751c9f097d9d866605306bbdd4b96e2a5c4e (patch)
tree1838365e6ad6592a26e193289d2136cb89319972 /examples/threads/waitconditions
parent5feefb0c039dae04290d1fca7c2f24276b6f7582 (diff)
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.cpp132
-rw-r--r--examples/threads/waitconditions/waitconditions.pro5
2 files changed, 99 insertions, 38 deletions
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 <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.pro b/examples/threads/waitconditions/waitconditions.pro
index 53350d2dae..e326afa081 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]/qtbase/threads/waitconditions
INSTALLS += target sources
symbian: CONFIG += qt_example
+maemo5: CONFIG += qt_example
+
+simulator: warning(This example might not fully work on Simulator platform)