(Qt6 : 16-4) Thread간 signals and slogs 이용

Posted by : at

Category : Qt


#include <QCoreApplication>
#include <QThread>
#include "producer.h"
#include "consumer.h"

void cleanup()
{
    qInfo() << "Cleanup";
}

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

    QThread pthread;
    pthread.setObjectName("Producer Thread");
    Producer *producer = new Producer();
    producer->moveToThread(&pthread);
    QObject::connect(&pthread,&QThread::started,producer,&Producer::start);

    QThread cthread;
    cthread.setObjectName("Consumer Thread");
    Consumer *consumer = new Consumer();
    consumer->moveToThread(&cthread);

    QObject::connect(producer,&Producer::produce,consumer,&Consumer::consume);
    QObject::connect(producer,&Producer::finished,&pthread,&QThread::quit);
    QObject::connect(&pthread,&QThread::finished,&cthread,&QThread::quit);
    QObject::connect(&cthread,&QThread::finished,consumer,&Consumer::deleteLater);
    QObject::connect(&pthread,&QThread::finished,producer,&Producer::deleteLater);
    QObject::connect(&cthread,&QThread::finished,&a,&QCoreApplication::quit);
    QObject::connect(&a,&QCoreApplication::aboutToQuit,&cleanup);

    cthread.start();
    pthread.start();

    return a.exec();
}

About Taehyung Kim

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

Star
Useful Links