2011-12-16 2 views
0

다음 코드는 충돌하고 심지어 리눅스에 매달려 있습니다.eventlist에 이벤트가 너무 많으면 응용 프로그램이 충돌하는 이유는 무엇입니까?

#include <QCoreApplication> 
#include <QString> 
#include <QMap> 
#include <QList> 
#include <QDebug> 
#include <QThread> 
#include <QTest> 


long long emited=0; 
long long handled=0; 
int flag=0; 

class A:public QThread 
{ 
    Q_OBJECT 
public: 
    A(QString n):n_(n){moveToThread(this);} 
    void run() { 
     QMetaObject::invokeMethod(this, "g",Qt::QueuedConnection); 
     exec(); 
    } 
    QString n_; 
signals: 
    void as(); 
public slots: 
    void g(){ 
     while(1) { 
      ++emited; 
      emit as(); 
     } 
    } 
}; 

class Main:public QObject 
{ 
    Q_OBJECT 
public slots: 
    void s0(){} 
    void s1(){ 
     ++flag; 
     ++handled; 
     A *obj = qobject_cast<A*>(sender()); 
     int nothandle=emited-handled; 
     --flag; 
     if(obj) { 
      qDebug()<<"s1"<<obj->n_<<"not handled:"<<nothandle<<flag; 
     } 
    } 
}; 



int main(int argc, char *argv[]) 
{ 
    QCoreApplication a(argc, argv); 
    QThread th1,th2; 
    A a1("a1"),a2("a2"); 
    Main m; 
    QObject::connect(&a1,SIGNAL(as()),&m,SLOT(s1()),Qt::QueuedConnection); 
    QObject::connect(&a2,SIGNAL(as()),&m,SLOT(s1()),Qt::QueuedConnection); 
    a1.start(); 
    a2.start(); 
    return a.exec(); 
} 
+2

오 소년 ... 하지마 ... 진짜로 움직이지 마. 토 (이)! 정말 틀렸어. http://labs.qt.nokia.com/2010/06/17/youre-doing-it-wrong/ –

답변

2

그것은이 때문에 충돌이 :

while(1) { 
    ++emited; 
    emit as(); 
} 

Qt는 신호 큐는 성장하지만 Qt는셔서되지 않는 것은 신호를 처리 계속

, 그래서 그것이 충돌까지가는 유지합니다. Qtimer를 사용하여 응용 프로그램을 정지 시키거나 Qt가 신호를 처리하게하십시오.

관련 문제