diff options
author | Timothy Pearson <kb9vqf@pearsoncomputing.net> | 2011-11-08 12:31:36 -0600 |
---|---|---|
committer | Timothy Pearson <kb9vqf@pearsoncomputing.net> | 2011-11-08 12:31:36 -0600 |
commit | d796c9dd933ab96ec83b9a634feedd5d32e1ba3f (patch) | |
tree | 6e3dcca4f77e20ec8966c666aac7c35bd4704053 /examples/thread/semaphores | |
download | tqt3-d796c9dd933ab96ec83b9a634feedd5d32e1ba3f.tar.gz tqt3-d796c9dd933ab96ec83b9a634feedd5d32e1ba3f.zip |
Test conversion to TQt3 from Qt3 8c6fc1f8e35fd264dd01c582ca5e7549b32ab731
Diffstat (limited to 'examples/thread/semaphores')
-rw-r--r-- | examples/thread/semaphores/main.cpp | 274 | ||||
-rw-r--r-- | examples/thread/semaphores/semaphores.pro | 10 |
2 files changed, 284 insertions, 0 deletions
diff --git a/examples/thread/semaphores/main.cpp b/examples/thread/semaphores/main.cpp new file mode 100644 index 000000000..69d4779d5 --- /dev/null +++ b/examples/thread/semaphores/main.cpp @@ -0,0 +1,274 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for TQt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ +#include <qapplication.h> +#include <qwidget.h> +#include <qpushbutton.h> +#include <qmultilineedit.h> +#include <qthread.h> +#include <qsemaphore.h> +#include <qmutex.h> +#include <qlayout.h> +#include <qmessagebox.h> +#include <qlabel.h> + +#if defined(QT_NO_THREAD) +# error Thread support not enabled. +#endif + +// Use pointers to create semaphores after TQApplication object! +TQSemaphore* yellowSem, *greenSem; + + +class YellowThread : public TQThread +{ +public: + YellowThread(TQWidget *o) + : receiver(o), stopped(FALSE) + { ; } + + void run(); + void stop(); + + +private: + TQWidget *receiver; + TQMutex mutex; + bool stopped; +}; + + +void YellowThread::run() +{ + for (int i = 0; i < 20; i++) { + (*yellowSem)++; + + TQCustomEvent *event = new TQCustomEvent(12345); + event->setData(new TQString("Yellow!")); + TQApplication::postEvent(receiver, event); + msleep(200); + + (*greenSem)--; + + mutex.lock(); + if (stopped) { + stopped = FALSE; + mutex.unlock(); + break; + } + mutex.unlock(); + } + + (*yellowSem)++; + + TQCustomEvent *event = new TQCustomEvent(12346); + event->setData(new TQString("Yellow!")); + TQApplication::postEvent(receiver, event); + + (*greenSem)--; +} + +void YellowThread::stop() +{ + mutex.lock(); + stopped = TRUE; + mutex.unlock(); +} + + +class GreenThread: public TQThread +{ +public: + GreenThread(TQWidget *o) + : receiver(o), stopped( FALSE ) + { ; } + + void run(); + void stop(); + + +private: + TQWidget *receiver; + TQMutex mutex; + bool stopped; +}; + + +void GreenThread::run() +{ + for (int i = 0; i < 20; i++) { + (*greenSem)++; + + TQCustomEvent *event = new TQCustomEvent(12345); + event->setData(new TQString("Green!")); + TQApplication::postEvent(receiver, event); + msleep(200); + + (*yellowSem)--; + + mutex.lock(); + if (stopped) { + stopped = FALSE; + mutex.unlock(); + break; + } + mutex.unlock(); + } + + (*greenSem)++; + + TQCustomEvent *event = new TQCustomEvent(12346); + event->setData(new TQString("Green!")); + TQApplication::postEvent(receiver, event); + msleep(10); + + (*yellowSem)--; +} + +void GreenThread::stop() +{ + mutex.lock(); + stopped = TRUE; + mutex.unlock(); +} + + + +class SemaphoreExample : public TQWidget +{ + Q_OBJECT +public: + SemaphoreExample(); + ~SemaphoreExample(); + + void customEvent(TQCustomEvent *); + + +public slots: + void startExample(); + + +protected: + + +private: + TQMultiLineEdit *mlineedit; + TQPushButton *button; + TQLabel *label; + + YellowThread yellowThread; + GreenThread greenThread; +}; + + +SemaphoreExample::SemaphoreExample() + : TQWidget(), yellowThread(this), greenThread(this) +{ + yellowSem = new TQSemaphore(1); + greenSem = new TQSemaphore(1); + + button = new TQPushButton("&Ignition!", this); + connect(button, SIGNAL(clicked()), SLOT(startExample())); + + mlineedit = new TQMultiLineEdit(this); + label = new TQLabel(this); + + TQVBoxLayout *vbox = new TQVBoxLayout(this, 5); + vbox->addWidget(button); + vbox->addWidget(mlineedit); + vbox->addWidget(label); +} + + +SemaphoreExample::~SemaphoreExample() +{ + bool stopYellow = yellowThread.running(), + stopGreen = greenThread.running(); + if (stopYellow) + yellowThread.stop(); + if (greenThread.running()) + greenThread.stop(); + if (stopYellow) + yellowThread.wait(); + if (stopGreen) + greenThread.wait(); + delete yellowSem; + delete greenSem; +} + + +void SemaphoreExample::startExample() +{ + if (yellowThread.running() || greenThread.running()) { + TQMessageBox::information(this, "Sorry", + "The threads have not completed yet, and must finish before " + "they can be started again."); + + return; + } + + mlineedit->clear(); + + while (yellowSem->available() < yellowSem->total()) (*yellowSem)--; + (*yellowSem)++; + + yellowThread.start(); + greenThread.start(); +} + + +void SemaphoreExample::customEvent(TQCustomEvent *event) { + switch (event->type()) { + case 12345: + { + TQString *s = (TQString *) event->data(); + + mlineedit->append(*s); + + if (*s == "Green!") + label->setBackgroundColor(green); + else + label->setBackgroundColor(yellow); + label->setText(*s); + + delete s; + + break; + } + + case 12346: + { + TQString *s = (TQString *) event->data(); + + TQMessageBox::information(this, (*s) + " - Finished", + "The thread creating the \"" + *s + + "\" events has finished."); + delete s; + + break; + } + + default: + { + qWarning("Unknown custom event type: %d", event->type()); + } + } +} + + +int main(int argc, char **argv) +{ + TQApplication app(argc, argv); + SemaphoreExample se; + app.setMainWidget(&se); + se.show(); + return app.exec(); +} + + +#include "main.moc" diff --git a/examples/thread/semaphores/semaphores.pro b/examples/thread/semaphores/semaphores.pro new file mode 100644 index 000000000..7804ce341 --- /dev/null +++ b/examples/thread/semaphores/semaphores.pro @@ -0,0 +1,10 @@ +TEMPLATE = app +TARGET = semaphores + +CONFIG += qt warn_on release thread + +REQUIRES = thread full-config + +HEADERS = +SOURCES = main.cpp +INTERFACES = |