Congratulations to our 2022 Qt Champions!

QApplication is lazy (or making other threads lazy in the app)

  • Hi all,

    This is my first post here and I hope to find a solution to my problem. I have started developing an app for Mac. I am facing a huge and frustrating problem right now.

    My problem is QApplication event loop becomes lazy (or makes other threads lazy in the app) after 20-50 seconds. I tried to replicate the same problem and came up with the code below.

    So here is what I do. I create a c++ new thread and the new thread prints the current time in every 2 seconds. The problem is after 10-30 iterations, some iterations take 6-12 seconds which shouldn't happen because I just sleep 2 seconds in every iteration. I ran the code below and the output is like this:

    sumits-air:UbiqMac_qt Jay$ ./
    "05.06.2015 16:43:30"
    "05.06.2015 16:43:32"
    "05.06.2015 16:43:34"
    "05.06.2015 16:43:36"
    "05.06.2015 16:43:38"
    "05.06.2015 16:43:40"
    "05.06.2015 16:43:42"
    "05.06.2015 16:43:44"
    "05.06.2015 16:43:46"
    "05.06.2015 16:43:48"
    "05.06.2015 16:43:50"
    "05.06.2015 16:43:52"
    "05.06.2015 16:43:54"
    "05.06.2015 16:43:56"
    "05.06.2015 16:43:58"
    "05.06.2015 16:44:00"
    "05.06.2015 16:44:02"
    "05.06.2015 16:44:04"
    "05.06.2015 16:44:06" (- 06 here)
    "05.06.2015 16:44:18" (- 18 here. 12 seconds difference)
    "05.06.2015 16:44:24" (- 24 here. 6 seconds difference)
    "05.06.2015 16:44:26"
    "05.06.2015 16:44:28"
    "05.06.2015 16:44:30"
    sumits-air:UbiqMac_qt Jay$

    When I run this program, every single time the same problem happens. I am not sure if the same problem will if someone else tries to do it. But it happens in my machine.

    The code below without QApplication works fine. So please do not blame c++ thread or usleep or the kernel for thread management or so. The other strange thing is that when I use QCoreApplication instead of QApplication it works fine as well. In addition, I use the same code in ubuntu based machine and it works fine with QApplication. I guess this only happens in Mac (I haven't tried windows though).

    Please do not suggest using QThread, QTimer or QTimer::singleShot. I was using them at first and having the same problem. I was using signals with QTimer and QThread and the problem was that signals were not emitted in time. The latency was similar (6 - 12 seconds). Actually, that is why I am using c++ thread beucase I thought using c++ thread may solve the problem but it didn't.

    Any help is appreciated.

    OS: MAC OSX 10.9.5.

    uname -a output:
    Darwin sumits-air 13.4.0 Darwin Kernel Version 13.4.0: Sun Aug 17 19:50:11 PDT 2014; root:xnu-2422.115.4~1/RELEASE_X86_64 x86_64

    The Code:

    #include <QApplication>
    #include <QDebug>
    #include <QDateTime>

    #include <unistd.h>
    #include <stdio.h>
    #include <time.h>
    #include <thread>

    void test() {
    while(true) {
    qDebug() << QDateTime::currentDateTime().toString("dd.MM.yyyy hh:mm:ss");

    int main(int argc, char *argv[]) {
    QApplication a(argc, argv);
    std::thread *heartbeatThread = new std::thread(&test);
    return 0;

    QMAKE_CXXFLAGS += -std=c++11
    QMAKE_CXXFLAGS += -stdlib=libc++
    LIBS += -stdlib=libc++
    QT += core gui widgets
    TARGET = test
    TEMPLATE = app
    SOURCES += main.cpp

  • I asked the same quesion in stackoverflow. I gues I found out why I am having this problem.