#include <QApplication>
#include <QThread>
#include <QMutex>
#include <QMessageBox>

#include <QtDebug>

//inherating QThread class
class ExTThread:public QThread
{
public:
ExTThread (QString text);

void run (); //method for run

private:
// m_text is a string varible
QString mv_text;
};

bool stopThreads = false; //Declaring Bool value

ExTThread::ExTThread (QString text):QThread ()
{
mv_text = text; // text isassigned to mv_text
}

QMutex
mutex;
//definition of run method declared in class ExTThread
void
ExTThread::run ()
{
while (!stopThreads)
{
mutex.lock ();
if (stopThreads)
return;

qDebug () << mv_text;
sleep (5);
mutex.unlock ();
}
}

int
main (int argc, char **argv)
{
QApplication app (argc, argv);

ExTThread foo ("Fo1"), ba1 ("Ba1");

fo1.start (); //start fo1

ba1.start (); //start ba1

QMessageBox::information (0, "Threading", "run after 5 sec close to stop");

stopThreads = true;

fo1.wait ();
ba1.wait ();

return 0;
}

This sample code is written by Mr. Vinay Pathak. To contribute an article send your concept as a article at
myconcept@wikistack.com



Related Contents to follow