aboutsummaryrefslogtreecommitdiff
path: root/sandbox/wait-test/main.cpp
blob: e2c1a4cea860411bc4ca6ede086e867a046f12cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <QTimer>
#include <QDebug>
#include <QWaitCondition>
#include <QMutex>
#include <QCoreApplication>

int i=0;

class MyTimer:public QTimer
{
    Q_OBJECT;
public:
    MyTimer()
        {
            connect(this, SIGNAL(timeout()),
                    this, SLOT(timedout()));
            start(10000);
            setSingleShot(true);
        }
public Q_SLOTS:
    void timedout()
        {
            qDebug() << "timed out, wake up everyone";
            i=1;
        }
};

class MyTimerWaiter:public QTimer
{
    Q_OBJECT;
public:
    MyTimerWaiter()
        {
            connect(this, SIGNAL(timeout()),
                    this, SLOT(timedout()));
            start(0);
        }
public Q_SLOTS:
    void timedout()
        {
            qDebug() << "timed out, so starting to wait";
            while(i == 0) {
                qDebug() << "I'm entering processEvents";
                QCoreApplication::processEvents(QEventLoop::WaitForMoreEvents);
                qDebug() << "I've been exited from processEvents";
                usleep(1000000);
            }

            qDebug() << "I've been awakened";
            exit(1);
        }
};

int main(int argc, char **argv)
{
    QCoreApplication app(argc, argv);

//    MyTimerWaiter waiter;
    MyTimer timer;

    while(i == 0) {
        qDebug() << "I'm entering processEvents";
        QCoreApplication::processEvents(QEventLoop::WaitForMoreEvents);
        qDebug() << "I've been exited from processEvents";
    }

    qDebug() << "NOT starting the main loop";
    return 0;

    return app.exec();
}

#include "main.moc"