18 Eylül 2018 Salı

Mutex with QT

*mutex kullanımı
//myhtread.h
#ifndef MYTHREAD_H
#define MYTHREAD_H
#include < QtCore >    

class MyThread:public QThread
{
public:
    MyThread();
    void run();
    QString name;
    bool Stop;
};

#endif // MYTHREAD_H




//main.cpp
#include < QCoreApplication >
#include "mythread.h"
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    MyThread m1;
    m1.name="first thread";

    MyThread m2;
    m2.name="second thread";

    MyThread m3;
    m3.name="third thread";

    m1.start(QThread::HighestPriority);
  //  m2.start(QThread::LowestPriority);
  //  m3.start();
    m1.Stop =true;
    
    return a.exec();
}



// thread.cp
#include "mythread.h"
#include < QtCore >  // UNUTULMAMALI CORE KULLANILACAK 
#include< QDebug >

#include < QThread >
MyThread::MyThread()
{

}

void MyThread::run()
{

    for(int i=0;i<10;i++)
    {
        QMutex mutex;
        if(this->Stop)
        {
            break;
        }
        mutex.unlock();
        this->sleep(1);
        qDebug() << "Runnning"<<name <<"   : "<< i;

    }
    
}

Share: