C++ Class
2019.12.25 14:52

QThread 소개 및 예제

조회 수 34288 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

QThread를 이용한 스레드 생성에는 몇가지 방법이 있다. 간단한 예제를 통해서 하나씩 알아본다.

 

worker-object를 QThread객체로 이동(move To Thread)하여 사용하는 방법

 

worker.h

#include <QObject>

class Worker : public QObject
{
    Q_OBJECT
public:
    explicit Worker(QObject *parent = nullptr);
    virtual ~Worker();

public slots:
    void doWork(const QString &);

signals:
    void start(const QString &);
    void resultReady(const QString &result);

};

 

worker.cpp

#include "worker.h"

Worker::Worker(QObject *parent) : QObject(parent)
{

}

Worker::~Worker()
{
    qDebug() << Q_FUNC_INFO;
}

void Worker::doWork(const QString &meter) {
    QString result("hello");

    // 여기서 오래걸리는 작업을 수행

    emit resultReady(result + parameter);
}

 

main.cpp

#include "worker.h"

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

    QThread thread;

    Worker *worker = new Worker;
    worker->moveToThread(&thread);
    thread.start();

    QObject::connect(worker, &Worker::start, worker, &Worker::doWork);
    QObject::connect(&thread, &QThread::finished, worker, &QObject::deleteLater);

    QObject::connect(worker, &Worker::resultReady, [&](const QString &result){
        qDebug() << result;
        thread.quit(); // 스레드중지
    });

    emit worker->start("World");

    return a.exec();
}

 

QThread를 서브클래싱하여 run() 함수를 다시 구현하는 방법

 

mythread.h

#include <QThread>

class MyThread : public QThread
{
    Q_OBJECT
public:
    MyThread();
    ~MyThread() override;

protected:
    void run() override;
};

 

mythread.cpp

#include <QDebug>
#include "mythread.h"

MyThread::MyThread()
{

}

MyThread::~MyThread()
{
    qDebug() << Q_FUNC_INFO;
}

void MyThread::run()
{
    forever{ // for(;;)와 동일. 무한 루프를 작성하는 데 편의를 제공하는 매크로.

        // 여기서 오래걸리는 코드를 실행

        // 스레드에서 실행 중인 작업을 중지해야 하는 경우 true가 반환됨. 중단은 requestInterruption() 함수로 요청할 수 있다.
        if ( QThread::currentThread()->isInterruptionRequested() ) {
            qDebug() << Q_FUNC_INFO << " terminated";
            return;
        }
    }
}

 

main.cpp

#include "mythread.h"

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

    MyThread *thread = new MyThread;
    QObject::connect(thread, &MyThread::finished, thread, &QObject::deleteLater);

    // 스레드 시작
    thread->start();

    QTimer::singleShot(3000, &a, [thread](){

        // 3초후에 스레드 중단을 요청.
        thread->requestInterruption();
    });

    return a.exec();
}

 

create() 정적 메서드를 호출하는 방법

Qt 5.10부터는 create() 정적 메서드를 호출하여 스레드를 생성할 수 있다.

#include <QCoreApplication>
#include <QThread>
#include <QDebug>

void f(){
    forever{

        // 여기서 오래걸리는 작업을 수행

        if ( QThread::currentThread()->isInterruptionRequested() ) {
            return;
        }
    }
}

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

    auto thread = QThread::create(f); // 함수 f를 실행할 새로운 QThread 객체를 만든다.

    thread->start(); // 스레드 시작

    return a.exec();
}

 

 

QThread는 스레드가 시작될 때 started() 를, 중지될 때 finished() 신호를 통해 통지하고 isFinished() 및 isRunning()을 사용하여 스레드 상태를 알 수 있다.

 

스레드를 중지 할 때는 exit() 또는 quit()를 호출한다. 실행중인 스레드를 강제로 종료하는 terminate() 를 호출 할 수 있지만 그런 상황은 되도록이면 없어야한다.

exit() 또는 quit()를 호출한 다음에는 스레드가 실행을 완료 할 때까지 (또는 지정된 시간이 지날 때까지) wait()를 사용하여 호출 스레드를 차단하는 것이 좋다.

 

Qt 4.8부터는 finished() 신호를 QObject::deleteLater()에 연결하여 종료 한 스레드 객체를 안전하게 해제 할 수 있다.

 

또한 플랫폼 독립적인 정적 sleep 함수를 제공한다. sleep(), msleep() 및 usleep()은 각각 초, 밀리초 및 마이크로초를 단위의 함수들이다.

 

Qt는 이벤트 중심(event-driven) 프레임 워크이므로 일반적으로 wait() 및 sleep() 함수는 필요하지 않을 수 있다. wait() 대신 finished() 시그널을 이용하는 것을 고려하자.

TAG •

  1. No Image notice

    Qt프로그래밍(QtQuick) Beginner를 위한 글 읽는 순서

    Date2019.01.05 CategoryQML and Qt Quick By운영자 Views834892
    read more
  2. No Image

    QThread 소개 및 예제

    Date2019.12.25 CategoryC++ Class Bymakersweb Views34288
    Read More
  3. ApplicationWindow 와 메뉴바(MenuBar)구성

    Date2020.01.04 CategoryQML and Qt Quick Bymakersweb Views28230
    Read More
  4. No Image

    Qt 멀티 스레드 프로그래밍 시 유의해야 할 몇 가지

    Date2020.01.13 CategoryGeneral and Desktop Bymakersweb Views18380
    Read More
  5. No Image

    Qt로 XML 파싱 : Qt 6에서 업데이트된 (Parsing XML with Qt: Updates for Qt 6)

    Date2020.01.16 CategoryQt 6 Byj2doll Views34583
    Read More
  6. No Image

    2020년에 변경되는 Qt 오퍼 (Qt offering changes 2020)

    Date2020.01.31 CategoryGeneral and Desktop Byj2doll Views22744
    Read More
  7. QOpenGLWidget 을 투명하게 적용

    Date2020.02.05 CategoryGeneral and Desktop Bymakersweb Views16272
    Read More
  8. 라즈베리파이4에 대한 Qt 5.14.1 크로스컴파일

    Date2020.02.12 CategoryMobile and Embedded Bymakersweb Views18829
    Read More
  9. No Image

    QLabel의 텍스트 색과 배경색을 변경

    Date2020.02.25 CategoryC++ Class Bymakersweb Views20232
    Read More
  10. Qt로 작성된 안드로이드 APP에 Splash Screen을 추가

    Date2020.03.10 CategoryMobile and Embedded Bymakersweb Views22595
    Read More
  11. No Image

    컨테이너 클래스 - QVector

    Date2020.03.17 CategoryC++ Class Bymakersweb Views14933
    Read More
  12. 콘솔에서 사용자 입력받기

    Date2020.03.22 CategoryGeneral and Desktop Bymakersweb Views65727
    Read More
  13. No Image

    Qt 5.15 및 Qt 6의 출시 일정

    Date2020.04.09 CategoryQt 6 Bymakersweb Views17622
    Read More
  14. No Image

    재진입(Reentrancy) 및 스레드 안전성(Thread-Safety)

    Date2020.04.19 CategoryC++ Class Bymakersweb Views17347
    Read More
  15. Qt기반의 서버와 클라이언트간 SOAP(Simple Object Access Protocol)

    Date2020.05.11 CategoryGeneral and Desktop Bymakersweb Views25519
    Read More
  16. ShaderEffect QML Type을 이용한 버튼 클릭 효과

    Date2020.05.22 CategoryQML and Qt Quick Bymakersweb Views19548
    Read More
  17. Embedded Linux 에서 Qt 및 Graphics Stack

    Date2020.05.27 By운영자 Views16470
    Read More
  18. Qt MQTT 에 대해서

    Date2020.06.02 CategoryMobile and Embedded Bymakersweb Views18575
    Read More
  19. 최초의 Qt 6.0 스냅샷 제공 (First Qt 6.0 Snapshot Available)

    Date2020.06.21 CategoryQt 6 Byj2doll Views19195
    Read More
  20. No Image

    Qt로 데이터를 직렬화(serialization)하는 방법

    Date2020.08.04 CategoryC++ Class Bymakersweb Views15922
    Read More
  21. UI 폼(Form)작성 시 탭 순서(Tab Order) 설정

    Date2020.08.24 CategoryGeneral and Desktop Bymakersweb Views19577
    Read More
Board Pagination Prev 1 4 5 6 7 8 9 Next
/ 9