18 Eylül 2018 Salı

QtConcurrent Kullanımı

QtConcurrent  mutex,semaphore gibi primitive yapıları kullanmadan yüksek seviyeli uygulama yazmamıza olanak saglar 

QTConcurrent ile yazılmış uygulamalar uygun olan şekilde core otomatik ayarlanır 


.proya  QT += concurrent eklenir

// main. cpp 
#include <    QCoreApplication             >
#include <    QtConcurrent/QtConcurrent    >
#include <    QThread                      >
#ifndef QT_NO_CONCURRENT

void myRunFunction(QString name)
{
    for(int i=0;i<10 ;i++)
    {
        qDebug()<<  name << " from   "  << QThread::currentThread();
    }
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QFuture<  void  > t1 = QtConcurrent::run(myRunFunction ,QString("A"));
    QFuture<  void  > t2 = QtConcurrent::run(myRunFunction ,QString("B"));
    QFuture<  void  > t3 = QtConcurrent::run(myRunFunction ,QString("C"));

    t1.waitForFinished();
    t2.waitForFinished();
    t3.waitForFinished();


    return a.exec();
}

#else
#include 

int main(int argc, char *argv[])
{
    QApplication app(argc,argv);
    QString text("QtConcurrent is not yet supported on this platform");
    QLabel *label =new QLabel(text);
    qDebug()<
    app.exec();
}
#endif

Share: