PDA

View Full Version : Thread



chethana
16th October 2007, 06:51
Hi All,,

If any one have study material related to Thread and simple programs related to thread then please send ... i am not feeling it sufficient what ever they have given in the Qt Assistant ...



Thanking you All...

rajesh
16th October 2007, 07:24
Have you seen this following 3 examples which is given in Qt Assistance?

http://doc.trolltech.com/4.3/threads-mandelbrot.html
http://doc.trolltech.com/4.3/threads-semaphores.html
http://doc.trolltech.com/4.3/threads-waitconditions.html

nile.one
16th October 2007, 07:30
QThread's documentation and "Thread Support in Qt" article given in QAssistant are enough to realize threads.



Creating a Thread
To create a thread, subclass QThread and reimplement its run() function. For example:
class MyThread : public QThread
{
Q_OBJECT

protected:
void run();
};

void MyThread::run()
{
...
}
Then, create an instance of the thread object and call QThread::start(). The code that appears in the run() reimplementation will then be executed in a separate thread.

So, main features to know:
1. create subclass of QThread
2. reimplement run()
3. execute thread buy calling start(). then thread starts and running run(). Execution ends when you return from run().

ashukla
16th October 2007, 07:53
Dear Asker,

This code is available in the book
C++ GUI Programming with Qt 4
By Jasmin Blanchette, Mark Summerfield


This is free online book!


Creating Threads
Providing multiple threads in a Qt application is straightforward: We just subclass QThread and reimplement its run() function. To show how this works, we will start by reviewing the code for a very simple QThread subclass that repeatedly prints a given string on a console.

class Thread : public QThread
{
Q_OBJECT
public:
Thread();
void setMessage(const QString &message);
void stop();
protected:
void run();
private:
QString messageStr;
volatile bool stopped;
};

The Thread class inherits from QThread and reimplements the run() function. It provides two additional functions: setMessage() and stop().
The stopped variable is declared volatile because it is accessed from different threads and we want to be sure that it is freshly read every time it is needed. If we omitted the volatile keyword, the compiler might optimize access to the variable, possibly leading to incorrect results.

Thread::Thread()
{
stopped = false;
}

We set stopped to false in the constructor.

void Thread::run()
{
while (!stopped)
cerr << qPrintable(messageStr);
stopped = false;
cerr << endl;
}

The run() function is called to start executing the thread. As long as the stopped variable is false, the function keeps printing the given message to the console. The thread terminates when control leaves the run() function.

void Thread::stop()
{
stopped = true;
}

The stop() function sets the stopped variable to true, thereby telling run() to stop printing text to the console. This function can be called from any thread at any time. For the purposes of this example, we assume that assignment to a bool is an atomic operation. This is a reasonable assumption, considering that a bool can only have two states. We will see later in this section how to use QMutex to guarantee that assigning to a variable is an atomic operation.
QThread provides a terminate() function that terminates the execution of a thread while it is still running. Using terminate() is not recommended, since it can stop the thread at any point and does not give the thread any chance to clean up after itself. It is always safer to use a stopped variable and a stop() function as we did here.
Figure 18.1. The Threads application

We will now see how to use the THRead class in a small Qt application that uses two threads, A and B, in addition to the main thread.

class ThreadDialog : public QDialog
{
Q_OBJECT
public:
ThreadDialog(QWidget *parent = 0);
protected:
void closeEvent(QCloseEvent *event);
private slots:
void startOrStopThreadA();
void startOrStopThreadB();
private:
Thread threadA;
Thread threadB;
QPushButton *threadAButton;
QPushButton *threadBButton;
QPushButton *quitButton;
};

The ThreadDialog class declares two variables of type Thread and some buttons to provide a basic user interface.

ThreadDialog::ThreadDialog(QWidget *parent)
: QDialog(parent)
{
threadA.setMessage("A");
threadB.setMessage("B");
threadAButton = new QPushButton(tr("Start A"));
threadBButton = new QPushButton(tr("Start B"));
quitButton = new QPushButton(tr("Quit"));
quitButton->setDefault(true);
connect(threadAButton, SIGNAL(clicked()),
this, SLOT(startOrStopThreadA()));
connect(threadBButton, SIGNAL(clicked()),
this, SLOT(startOrStopThreadB()));
...
}

In the constructor, we call setMessage() to make the first thread repeatedly print 'A's and the second thread 'B's.

void ThreadDialog::startOrStopThreadA()
{
if (threadA.isRunning()) {
threadA.stop();
threadAButton->setText(tr("Start A"));
} else {
threadA.start();
threadAButton->setText(tr("Stop A"));
}
}

When the user clicks the button for thread A, startOrStopThreadA() stops the thread if it was running and starts it otherwise. It also updates the button's text.

void ThreadDialog::startOrStopThreadB()
{
if (threadB.isRunning()) {
threadB.stop();
threadBButton->setText(tr("Start B"));
} else {
threadB.start();
threadBButton->setText(tr("Stop B"));
}
}

The code for startOrStopThreadB() is very similar.

void ThreadDialog::closeEvent(QCloseEvent *event)
{
threadA.stop();
threadB.stop();
threadA.wait();
threadB.wait();
event->accept();
}

If the user clicks Quit or closes the window, we stop any running threads and wait for them to finish (using QThread::wait()) before we call QCloseEvent::accept(). This ensures that the application exits in a clean state, although it doesn't really matter in this example.
If you run the application and click Start A, the console will be filled with 'A's. If you click Start B, it will now fill with alternating sequences of 'A's and 'B's. Click Stop A, and now it will only print 'B's.

chethana
16th October 2007, 12:41
Hi All,


Thank you very much for sending the Information... But i have saw examples which are their in Examples and Demos in Qt Assistant ..

and also C++ GUI Programming with Qt4 in that book what ever the example they have i have tried it out...

Apart from that is their any material... or else simple examples which helps me in understanding QThread better way...

3nc31
28th November 2007, 02:21
Hi!!! Which are the headers files on the book's example? Because I try to compile and it fail!!:confused: ....and what types are "cerr" and "endl" ?? Thanks!!!

wysota
28th November 2007, 03:23
http://www.cppreference.com/cppio/index.html