2014-06-23 2 views

답변

3

QTimer의 timeout() 신호를 해당 슬롯에 연결하고 start()으로 전화 할 수 있습니다. 그 이후부터 타이머는 일정한 간격으로 timeout() 신호를 내 보냅니다. 그러나 두 타이머는 메인 스레드와 메인 이벤트 루프에서 실행됩니다. 따라서 다중 스레드라고 부를 수는 없습니다. 두 개의 슬롯이 동시에 실행되지 않기 때문입니다. 하나씩 실행하고 다른 스레드가 주 스레드를 차단하면 다른 스레드는 호출되지 않습니다.

당신은 동시에 수행하고 개체에 대한 스레드 선호도 변경 QObject::moveToThread를 사용해야 다른 작업에 대한 몇 가지 클래스함으로써 진정한 멀티 스레드 응용 프로그램을 가질 수 있습니다 QObject에서

QThread *thread1 = new QThread(); 
QThread *thread2 = new QThread(); 

Task1 *task1 = new Task1(); 
Task2 *task2 = new Task2(); 

task1->moveToThread(thread1); 
task2->moveToThread(thread2); 

connect(thread1, SIGNAL(started()), task1, SLOT(doWork())); 
connect(task1, SIGNAL(workFinished()), thread1, SLOT(quit())); 

connect(thread2, SIGNAL(started()), task2, SLOT(doWork())); 
connect(task2, SIGNAL(workFinished()), thread2, SLOT(quit())); 

//automatically delete thread and task object when work is done: 
connect(thread1, SIGNAL(finished()), task1, SLOT(deleteLater())); 
connect(thread1, SIGNAL(finished()), thread1, SLOT(deleteLater())); 

connect(thread2, SIGNAL(finished()), task2, SLOT(deleteLater())); 
connect(thread2, SIGNAL(finished()), thread2, SLOT(deleteLater())); 

thread1->start(); 
thread2->start(); 

Task1 그와 Task2 상속을 .

관련 문제