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. Why reading file in a separate thread is slowing down GUI?
Forum Updated to NodeBB v4.3 + New Features

Why reading file in a separate thread is slowing down GUI?

Scheduled Pinned Locked Moved Solved General and Desktop
qthreadguilagging
7 Posts 4 Posters 1.1k Views 3 Watching
  • 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.
  • CJhaC Offline
    CJhaC Offline
    CJha
    wrote on last edited by CJha
    #1

    Hi, I am reading a large file in a separate thread by using QThread::create method. My function to read the large file is in an anonymous namespace:

    namespace
    {
        void readFile()
        {
            QFile file{fileName}; // fileName is a constant QString
            if(file.open(QIODevice::ReadOnly)) {
                QTextStream stream{&file};
                const QString text = stream.readAll();
                textListMutex.lock();
                if(!text.isEmpty())
                    textList = text.split('\n', Qt::SkipEmptyParts); // textList is a QStringList
                textListMutex.unlock();
            }
        }
    }
    

    Upon receiving the input to read the file, which is by clicking a QPushButton, I do the following:

    auto thread = QThread::create(readFile);
    connect(thread, &QThread::finished, thread, &QObject::deleteLater);
    connect(thread, &QThread::destroyed, this, &Widget::readFileThreadDestroyed); // readFileThreadDestroyed() is a function to detect when reading is done
    thread->start();
    

    I check for QThread::currentThreadId() in both readFile() function and in my main class from where the command is issued. The function readFile() is being properly executed in a separate non-GUI thread. The GUI is not blocked but lagging severly i.e. it takes more than a few seconds (sometimes even 5 to 10 seconds) to respond to anything, but it does respond while the readFile() is still running in a separate thread, it's just that the response time is way slower than what it is when readFile() is not running in a separate thread. Why is this happening? Is there any way I could make my GUI thread respond faster while the readFile() is running in a separate thread? I tried changing the thread priority but that does not help at all.

    JonBJ CJhaC 2 Replies Last reply
    0
    • CJhaC CJha

      Hi, I am reading a large file in a separate thread by using QThread::create method. My function to read the large file is in an anonymous namespace:

      namespace
      {
          void readFile()
          {
              QFile file{fileName}; // fileName is a constant QString
              if(file.open(QIODevice::ReadOnly)) {
                  QTextStream stream{&file};
                  const QString text = stream.readAll();
                  textListMutex.lock();
                  if(!text.isEmpty())
                      textList = text.split('\n', Qt::SkipEmptyParts); // textList is a QStringList
                  textListMutex.unlock();
              }
          }
      }
      

      Upon receiving the input to read the file, which is by clicking a QPushButton, I do the following:

      auto thread = QThread::create(readFile);
      connect(thread, &QThread::finished, thread, &QObject::deleteLater);
      connect(thread, &QThread::destroyed, this, &Widget::readFileThreadDestroyed); // readFileThreadDestroyed() is a function to detect when reading is done
      thread->start();
      

      I check for QThread::currentThreadId() in both readFile() function and in my main class from where the command is issued. The function readFile() is being properly executed in a separate non-GUI thread. The GUI is not blocked but lagging severly i.e. it takes more than a few seconds (sometimes even 5 to 10 seconds) to respond to anything, but it does respond while the readFile() is still running in a separate thread, it's just that the response time is way slower than what it is when readFile() is not running in a separate thread. Why is this happening? Is there any way I could make my GUI thread respond faster while the readFile() is running in a separate thread? I tried changing the thread priority but that does not help at all.

      CJhaC Offline
      CJhaC Offline
      CJha
      wrote on last edited by
      #4

      @CJha My bad! solved it, it is because of QString QTextStream::readAll() function. The Qt document clearly states:

      Avoid this function when working on large files, as it will consume a significant amount of memory

      I assumed having enough memory and running this function in a separate thread would mean that it will work well without any problems but that is not the case. Instead, I am now using QString QTextStream::readLine(qint64 maxlen = 0) which makes the GUI smooth as butter ;)

      S 1 Reply Last reply
      4
      • CJhaC CJha

        Hi, I am reading a large file in a separate thread by using QThread::create method. My function to read the large file is in an anonymous namespace:

        namespace
        {
            void readFile()
            {
                QFile file{fileName}; // fileName is a constant QString
                if(file.open(QIODevice::ReadOnly)) {
                    QTextStream stream{&file};
                    const QString text = stream.readAll();
                    textListMutex.lock();
                    if(!text.isEmpty())
                        textList = text.split('\n', Qt::SkipEmptyParts); // textList is a QStringList
                    textListMutex.unlock();
                }
            }
        }
        

        Upon receiving the input to read the file, which is by clicking a QPushButton, I do the following:

        auto thread = QThread::create(readFile);
        connect(thread, &QThread::finished, thread, &QObject::deleteLater);
        connect(thread, &QThread::destroyed, this, &Widget::readFileThreadDestroyed); // readFileThreadDestroyed() is a function to detect when reading is done
        thread->start();
        

        I check for QThread::currentThreadId() in both readFile() function and in my main class from where the command is issued. The function readFile() is being properly executed in a separate non-GUI thread. The GUI is not blocked but lagging severly i.e. it takes more than a few seconds (sometimes even 5 to 10 seconds) to respond to anything, but it does respond while the readFile() is still running in a separate thread, it's just that the response time is way slower than what it is when readFile() is not running in a separate thread. Why is this happening? Is there any way I could make my GUI thread respond faster while the readFile() is running in a separate thread? I tried changing the thread priority but that does not help at all.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #2

        @CJha
        There may be some "general machine slowness" when doing this file reading, but I don't know of any reason why it should be "worse" if in a thread. I would start by (temporarily) commenting out the textListMutex code, need to eliminate possibility that it is waiting on a lock.

        CJhaC 1 Reply Last reply
        1
        • JonBJ JonB

          @CJha
          There may be some "general machine slowness" when doing this file reading, but I don't know of any reason why it should be "worse" if in a thread. I would start by (temporarily) commenting out the textListMutex code, need to eliminate possibility that it is waiting on a lock.

          CJhaC Offline
          CJhaC Offline
          CJha
          wrote on last edited by CJha
          #3

          @JonB I commented out the textListMutex and still the result is the same. My CPU usage is around 10% (I am using a quad-core Intel i5 11th gen CPU) while the readFile() function is executing. The file I am reading is a .txt file with a size of ~39 MB.

          1 Reply Last reply
          0
          • CJhaC CJha

            Hi, I am reading a large file in a separate thread by using QThread::create method. My function to read the large file is in an anonymous namespace:

            namespace
            {
                void readFile()
                {
                    QFile file{fileName}; // fileName is a constant QString
                    if(file.open(QIODevice::ReadOnly)) {
                        QTextStream stream{&file};
                        const QString text = stream.readAll();
                        textListMutex.lock();
                        if(!text.isEmpty())
                            textList = text.split('\n', Qt::SkipEmptyParts); // textList is a QStringList
                        textListMutex.unlock();
                    }
                }
            }
            

            Upon receiving the input to read the file, which is by clicking a QPushButton, I do the following:

            auto thread = QThread::create(readFile);
            connect(thread, &QThread::finished, thread, &QObject::deleteLater);
            connect(thread, &QThread::destroyed, this, &Widget::readFileThreadDestroyed); // readFileThreadDestroyed() is a function to detect when reading is done
            thread->start();
            

            I check for QThread::currentThreadId() in both readFile() function and in my main class from where the command is issued. The function readFile() is being properly executed in a separate non-GUI thread. The GUI is not blocked but lagging severly i.e. it takes more than a few seconds (sometimes even 5 to 10 seconds) to respond to anything, but it does respond while the readFile() is still running in a separate thread, it's just that the response time is way slower than what it is when readFile() is not running in a separate thread. Why is this happening? Is there any way I could make my GUI thread respond faster while the readFile() is running in a separate thread? I tried changing the thread priority but that does not help at all.

            CJhaC Offline
            CJhaC Offline
            CJha
            wrote on last edited by
            #4

            @CJha My bad! solved it, it is because of QString QTextStream::readAll() function. The Qt document clearly states:

            Avoid this function when working on large files, as it will consume a significant amount of memory

            I assumed having enough memory and running this function in a separate thread would mean that it will work well without any problems but that is not the case. Instead, I am now using QString QTextStream::readLine(qint64 maxlen = 0) which makes the GUI smooth as butter ;)

            S 1 Reply Last reply
            4
            • CJhaC CJha has marked this topic as solved on
            • CJhaC CJha

              @CJha My bad! solved it, it is because of QString QTextStream::readAll() function. The Qt document clearly states:

              Avoid this function when working on large files, as it will consume a significant amount of memory

              I assumed having enough memory and running this function in a separate thread would mean that it will work well without any problems but that is not the case. Instead, I am now using QString QTextStream::readLine(qint64 maxlen = 0) which makes the GUI smooth as butter ;)

              S Offline
              S Offline
              SimonSchroeder
              wrote on last edited by
              #5

              @CJha said in Why reading file in a separate thread is slowing down GUI?:

              Instead, I am now using QString QTextStream::readLine(qint64 maxlen = 0) which makes the GUI smooth as butter ;)

              Just a slight performance hint from my experience. Sure, your GUI is now running smoothly, but your separate thread could run a whole lot faster. Use QTextStream::readLineInto(QString*, qint64) instead. This can reuse an existing QString variable instead of allocating memory over and over again for each separate line.

              piervalliP CJhaC 2 Replies Last reply
              3
              • S SimonSchroeder

                @CJha said in Why reading file in a separate thread is slowing down GUI?:

                Instead, I am now using QString QTextStream::readLine(qint64 maxlen = 0) which makes the GUI smooth as butter ;)

                Just a slight performance hint from my experience. Sure, your GUI is now running smoothly, but your separate thread could run a whole lot faster. Use QTextStream::readLineInto(QString*, qint64) instead. This can reuse an existing QString variable instead of allocating memory over and over again for each separate line.

                piervalliP Do not disturb
                piervalliP Do not disturb
                piervalli
                wrote on last edited by
                #6

                @SimonSchroeder said in Why reading file in a separate thread is slowing down GUI?:

                Just a slight performance hint from my experience. Sure, your GUI is now running smoothly, but your separate thread could run a whole lot faster. Use QTextStream::readLineInto(QString*, qint64) instead.

                Power of the community,
                Thanks Thanks

                1 Reply Last reply
                0
                • S SimonSchroeder

                  @CJha said in Why reading file in a separate thread is slowing down GUI?:

                  Instead, I am now using QString QTextStream::readLine(qint64 maxlen = 0) which makes the GUI smooth as butter ;)

                  Just a slight performance hint from my experience. Sure, your GUI is now running smoothly, but your separate thread could run a whole lot faster. Use QTextStream::readLineInto(QString*, qint64) instead. This can reuse an existing QString variable instead of allocating memory over and over again for each separate line.

                  CJhaC Offline
                  CJhaC Offline
                  CJha
                  wrote on last edited by
                  #7

                  @SimonSchroeder Thank you! That is really good advice, I will try it out now :)

                  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