Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QApplication is lazy (or making other threads lazy in the app)

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

Scheduled Pinned Locked Moved General and Desktop
qapplicationqcoreapplicatioc++ threadqtimer
2 Posts 1 Posters 1.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    emustafa
    wrote on last edited by
    #1

    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$ ./run.sh
    "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"
    ^C
    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:
    main.cpp:

    #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");
    usleep(2000000);
    }
    }

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

    test.pro:

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

    1 Reply Last reply
    0
    • E Offline
      E Offline
      emustafa
      wrote on last edited by
      #2

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

      http://stackoverflow.com/questions/30686488/qapplication-is-lazy-or-making-other-threads-lazy-in-the-app

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved