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. [SOLVED]Delay function leads to Exception in new thread
QtWS25 Last Chance

[SOLVED]Delay function leads to Exception in new thread

Scheduled Pinned Locked Moved General and Desktop
delayexceptionthreadsqcoreapplicatioqeventloop
6 Posts 2 Posters 6.1k 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.
  • S Offline
    S Offline
    Sen Li
    wrote on last edited by Sen Li
    #1

    Hi,

    As a newbie working on Qt, the multi-Threads does give me a hard time.
    There is no problem with everything staying in the main thread; however, exceptions appear everywhere after I push my SerialObject into a new thread.

    Here is one of the function that leads to a big crash:

    void SenSerialControlAlgorithmObject::SenDelay(int msec)
    {
        QTime dieTime= QTime::currentTime().addMSecs(10*msec);//.addSecs(1);
        while( QTime::currentTime() < dieTime )
        QCoreApplication::processEvents(QEventLoop::AllEvents,msec); // 100 milliseconds
    }
    

    Could anyone tell me how to modify the QCoreApplication or QEventLoop so that this delay function can work well in a new thread?

    Thank you very much!

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #2

      If you want a synchronous delay of 10 * msecs then you should be able to use a QTimer and QEventLoop.

      void SenSerialControlAlgorithmObject::SenDelay(int msec)
      {
          QEventLoop eventLoop;
          QTimer::singleShot(10 * msecs, &eventLoop, SLOT(quit()));
          eventLoop.exec();
      }
      

      Generally though, you do not want a synchronous delay or threads if you can avoid them.

      As for the "exceptions appear everywhere", you haven't shared any of these so we can only guess that you have broken object ownership, unprotected shared data, or cross thread calls. Why do you need this in a thread at all?

      S 2 Replies Last reply
      0
      • C ChrisW67

        If you want a synchronous delay of 10 * msecs then you should be able to use a QTimer and QEventLoop.

        void SenSerialControlAlgorithmObject::SenDelay(int msec)
        {
            QEventLoop eventLoop;
            QTimer::singleShot(10 * msecs, &eventLoop, SLOT(quit()));
            eventLoop.exec();
        }
        

        Generally though, you do not want a synchronous delay or threads if you can avoid them.

        As for the "exceptions appear everywhere", you haven't shared any of these so we can only guess that you have broken object ownership, unprotected shared data, or cross thread calls. Why do you need this in a thread at all?

        S Offline
        S Offline
        Sen Li
        wrote on last edited by Sen Li
        #3

        @ChrisW67

        Thank you for you help!

        I need a Delay function for testing, not necessary.
        But I am wiling to learn how to fix the bug, and how to write a delay function in the new thread.

        Your codes also cause the exception, while comment select all of the delay function can fix everything,
        which means, your delay codes cannot work either in the new thread.

        There might be a thread-safe problem that I do not know.

        C 1 Reply Last reply
        0
        • C ChrisW67

          If you want a synchronous delay of 10 * msecs then you should be able to use a QTimer and QEventLoop.

          void SenSerialControlAlgorithmObject::SenDelay(int msec)
          {
              QEventLoop eventLoop;
              QTimer::singleShot(10 * msecs, &eventLoop, SLOT(quit()));
              eventLoop.exec();
          }
          

          Generally though, you do not want a synchronous delay or threads if you can avoid them.

          As for the "exceptions appear everywhere", you haven't shared any of these so we can only guess that you have broken object ownership, unprotected shared data, or cross thread calls. Why do you need this in a thread at all?

          S Offline
          S Offline
          Sen Li
          wrote on last edited by Sen Li
          #4

          @ChrisW67
          Here is another bug alert in Application Output while running my code in a new thread:

          Program: C:\Qt\5.4\msvc2013_opengl\bin\Qt5Cored.dll
          Module: 5.4.1
          File: global\qglobal.cpp
          Line: 2868
          
          ASSERT: "!isEmpty()" in file c:\work\build\qt5_workdir\w\s\qtbase\include\qtcore\../../src/corelib/tools/qlist.h, line 293
          
          (Press Retry to debug the application)
          
          ASSERT: "i >= 0" in file c:\work\build\qt5_workdir\w\s\qtbase\include\qtcore\../../src/corelib/tools/qstring.h, line 1020
          Debug Error!
          
          Program: C:\Qt\5.4\msvc2013_opengl\bin\Qt5Cored.dll
          Module: 5.4.1
          File: global\qglobal.cpp
          Line: 2868
          
          ASSERT: "i >= 0" in file c:\work\build\qt5_workdir\w\s\qtbase\include\qtcore\../../src/corelib/tools/qstring.h, line 1020
          
          (Press Retry to debug the application)
          
          Complete framesCount =  29
          ASSERT failure in QList<T>::at: "index out of range", file c:\work\build\qt5_workdir\w\s\qtbase\include\qtcore\../../src/corelib/tools/qlist.h, line 478
          Debug Error!
          
          Program: C:\Qt\5.4\msvc2013_opengl\bin\Qt5Cored.dll
          Module: 5.4.1
          File: global\qglobal.cpp
          Line: 2876
          
          ASSERT failure in QList<T>::at: "index out of range", file c:\work\build\qt5_workdir\w\s\qtbase\include\qtcore\../../src/corelib/tools/qlist.h, line 478
          
          (Press Retry to debug the application)
          

          However I cannot even find out which line cause it.

          Please tell me if you have any ideas.

          1 Reply Last reply
          0
          • S Sen Li

            @ChrisW67

            Thank you for you help!

            I need a Delay function for testing, not necessary.
            But I am wiling to learn how to fix the bug, and how to write a delay function in the new thread.

            Your codes also cause the exception, while comment select all of the delay function can fix everything,
            which means, your delay codes cannot work either in the new thread.

            There might be a thread-safe problem that I do not know.

            C Offline
            C Offline
            ChrisW67
            wrote on last edited by
            #5

            @Sen-Li said:

            @ChrisW67
            Your codes also cause the exception, while comment select all of the delay function can fix everything,
            which means, your delay codes cannot work either in the new thread.

            No, my code works fine to implement a synchronous delay in another thread. Your issues are almost certainly poor thread handling: hence my advice that they are rarely needed.

            #include <QCoreApplication>
            #include <QTimer>
            #include <QThread>
            #include <QObject>
            #include <QDebug>
            
            class Worker : public QObject {
            Q_OBJECT
            
            public:
                Worker() { }
                ~Worker() { }
            
            public slots:
                void process() {
                    qDebug() << "Thread starting";
                    for (int i = 0; i < 10; ++i) {
                        doSyncDelay(100);
                        qDebug() << "Delay finished" << i;
                    }
                    emit finished();
                    qDebug() << "Thread ending";
                }
            
            signals:
                void finished();
            
            private:
                void doSyncDelay(int msecs) {
                    QEventLoop eventLoop;
                    QTimer::singleShot(10 * msecs, &eventLoop, SLOT(quit()));
                    eventLoop.exec();
                }
            
            private:
                // add your variables here
            };
            
            int main(int argc, char ** argv) {
                QCoreApplication app(argc, argv);
            
                QThread* thread = new QThread(&app);
                Worker* worker = new Worker();
                worker->moveToThread(thread);
                QObject::connect(thread, SIGNAL(started()), worker, SLOT(process()));
                QObject::connect(worker, SIGNAL(finished()), thread, SLOT(quit()));
                QObject::connect(worker, SIGNAL(finished()), worker, SLOT(deleteLater()));
                QObject::connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
                thread->start();
            
                QTimer::singleShot(15000, &app, SLOT(quit()));
                return app.exec();
            }
            #include "main.moc"
            

            The exception in your last post is likey to be caused by your code using an out-of-bounds index into a QList in an at() or operator call.

            S 1 Reply Last reply
            0
            • C ChrisW67

              @Sen-Li said:

              @ChrisW67
              Your codes also cause the exception, while comment select all of the delay function can fix everything,
              which means, your delay codes cannot work either in the new thread.

              No, my code works fine to implement a synchronous delay in another thread. Your issues are almost certainly poor thread handling: hence my advice that they are rarely needed.

              #include <QCoreApplication>
              #include <QTimer>
              #include <QThread>
              #include <QObject>
              #include <QDebug>
              
              class Worker : public QObject {
              Q_OBJECT
              
              public:
                  Worker() { }
                  ~Worker() { }
              
              public slots:
                  void process() {
                      qDebug() << "Thread starting";
                      for (int i = 0; i < 10; ++i) {
                          doSyncDelay(100);
                          qDebug() << "Delay finished" << i;
                      }
                      emit finished();
                      qDebug() << "Thread ending";
                  }
              
              signals:
                  void finished();
              
              private:
                  void doSyncDelay(int msecs) {
                      QEventLoop eventLoop;
                      QTimer::singleShot(10 * msecs, &eventLoop, SLOT(quit()));
                      eventLoop.exec();
                  }
              
              private:
                  // add your variables here
              };
              
              int main(int argc, char ** argv) {
                  QCoreApplication app(argc, argv);
              
                  QThread* thread = new QThread(&app);
                  Worker* worker = new Worker();
                  worker->moveToThread(thread);
                  QObject::connect(thread, SIGNAL(started()), worker, SLOT(process()));
                  QObject::connect(worker, SIGNAL(finished()), thread, SLOT(quit()));
                  QObject::connect(worker, SIGNAL(finished()), worker, SLOT(deleteLater()));
                  QObject::connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
                  thread->start();
              
                  QTimer::singleShot(15000, &app, SLOT(quit()));
                  return app.exec();
              }
              #include "main.moc"
              

              The exception in your last post is likey to be caused by your code using an out-of-bounds index into a QList in an at() or operator call.

              S Offline
              S Offline
              Sen Li
              wrote on last edited by Sen Li
              #6

              @ChrisW67

              Thank you for you help.
              Probably the thing is, QSerialPort has a problem to work in a new thread.

              I can handle the other QObject in a new thread very well, and the exception only happens when the Object contains QSerialPort.
              Also, in my last post, I actually never user QList, although Qt tells me something wrong with QList.

              I have given up on working serialThread. QSerialPort does work good in the main thread.

              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