(Qt6 - Advanced) 4. QtConcurrent 시작

Posted by : at

Category : Qt


#include <QCoreApplication>
#include <QtConcurrent>

int do_map(int value)
{
    qInfo() << "Do stuff: " << value;
    int num = value * 10;
    return num;
}

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

    QList<int> values;
    for(int i = 0; i < 9; i++)
    {
        values << i;
    }

    qInfo() << "Starting...";
    QList<int> updated = QtConcurrent::blockingMapped(values,&do_map);
    qInfo() << "Finished";
    qInfo() << updated;


    return a.exec();
}

#include <QCoreApplication>
#include <QtConcurrent>
#include <QThread>
#include <QThreadPool>

void loop()
{
    for (int i = 0; i < 10;i++)
    {
        qInfo() << "Loop" << i << " on: " << QThread::currentThread();
    }
}

void test()
{
    qInfo() << "Test on: " << QThread::currentThread();
    loop();
}


int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QThread::currentThread()->setObjectName("Main");

    QThreadPool pool;
    QFuture<void> future = QtConcurrent::run(&pool,test);
    qInfo() << "Main on: " << QThread::currentThread();

    return a.exec();
}

#include <QCoreApplication>
#include <QtConcurrent>
#include <QThread>
#include <QFuture>

void test(QString name, int max)
{
    for (int i = 0;i < max;i++)
    {
        qInfo() << name << i << QThread::currentThread();
    }
}

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

    QString name = "name";

    QFuture<void> future = QtConcurrent::run(test,name,5);
    qInfo() << "Do stuff here";

    //Wait for finished
    future.waitForFinished(); //Block
    qInfo() << "Done";

    return a.exec();
}

About Taehyung Kim

안녕하세요? 8년차 현업 C++ 개발자 김태형이라고 합니다. 😁 C/C++을 사랑하며 다양한 사람과의 협업을 즐깁니다. ☕ 꾸준한 자기개발을 미덕이라 생각하며 노력중이며, 제가 얻은 지식을 홈페이지에 정리 중입니다. 좀 더 상세한 제 이력서 혹은 Private 프로젝트 접근 권한을 원하신다면 메일주세요. 😎

Star
Useful Links