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?

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

Scheduled Pinned Locked Moved Solved General and Desktop
qthreadguilagging
7 Posts 4 Posters 798 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.
  • C Offline
    C Offline
    CJha
    wrote on 18 Jul 2023, 09:36 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.

    J C 2 Replies Last reply 18 Jul 2023, 09:53
    0
    • C CJha
      18 Jul 2023, 09:36

      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.

      C Offline
      C Offline
      CJha
      wrote on 18 Jul 2023, 10:10 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 19 Jul 2023, 06:55
      4
      • C CJha
        18 Jul 2023, 09:36

        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.

        J Offline
        J Offline
        JonB
        wrote on 18 Jul 2023, 09:53 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.

        C 1 Reply Last reply 18 Jul 2023, 09:59
        1
        • J JonB
          18 Jul 2023, 09:53

          @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.

          C Offline
          C Offline
          CJha
          wrote on 18 Jul 2023, 09:59 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
          • C CJha
            18 Jul 2023, 09:36

            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.

            C Offline
            C Offline
            CJha
            wrote on 18 Jul 2023, 10:10 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 19 Jul 2023, 06:55
            4
            • C CJha has marked this topic as solved on 18 Jul 2023, 10:10
            • C CJha
              18 Jul 2023, 10:10

              @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 19 Jul 2023, 06:55 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 C 2 Replies Last reply 19 Jul 2023, 07:38
              3
              • S SimonSchroeder
                19 Jul 2023, 06:55

                @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 Offline
                piervalliP Offline
                piervalli
                wrote on 19 Jul 2023, 07:38 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
                  19 Jul 2023, 06:55

                  @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.

                  C Offline
                  C Offline
                  CJha
                  wrote on 19 Jul 2023, 08:22 last edited by
                  #7

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

                  1 Reply Last reply
                  0

                  1/7

                  18 Jul 2023, 09:36

                  • Login

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