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. Use Qt thread correctly
QtWS25 Last Chance

Use Qt thread correctly

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 3 Posters 3.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.
  • G Offline
    G Offline
    Galilio
    wrote on 6 Jul 2017, 08:40 last edited by
    #4

    At a class where I have the object "CAgilentLan"
    Calls I write in the contructor the following:

    //CAgilentLan* m_pvAgilentWorker; 
    m_pvWorkerThread = new QThread();
    m_pvAgilentWorker = new CAgilentLan(settings_l.strfuGetIP_DSO(), 500, settings_l.bofuGetSimulate());
    m_pvAgilentWorker->moveToThread(m_pvWorkerThread);
    connect(m_pvWorkerThread, &QThread::started, m_pvAgilentWorker, &CAgilentLan::agilentStart);
       
    connect(m_pvWorkerThread, &QThread::finished, m_pvWorkerThread, &QThread::deleteLater);
    connect(m_pvWorkerThread, &QThread::finished, m_pvWorkerThread, &CAgilentLan::deleteLater);
    m_pvWorkerThread->start();
    

    This is the error message:

    ASSERT failure in QCoreApplication::sendEvent: "Cannot send events to objects owned by a different thread. Current thread 4c19f68. Receiver '' (of type 'QNativeSocketEngine') was created in thread 4c582e0", file kernel\qcoreapplication.cpp, line 541
    
    1 Reply Last reply
    0
    • J jsulm
      6 Jul 2017, 08:05

      @Galilio This can be simplified:

      QByteArray CAgilentLan::GetLastResponse(void)
      {
      	QByteArray ay_l;
      
      	m_MtxResponse.lock();
      	ay_l = m_ByteArrayRead;
      	m_MtxResponse.unlock();
      
      	return ay_l;
      }
      

      to

      QByteArray CAgilentLan::GetLastResponse(void)
      {
      	QMutexLocker(m_MtxResponse);
      	return m_ByteArrayRead;
      }
      
      C Offline
      C Offline
      corruptedsyntax
      wrote on 6 Jul 2017, 14:09 last edited by
      #5

      @jsulm

      QByteArray CAgilentLan::GetLastResponse(void)
      {
          QMutexLocker(m_MtxResponse);
          return m_ByteArrayRead;
      }
      

      Won't that temporary QMutexLocker need a name? Otherwise it will be deconstructed as a temporary object at the end of the first statement and the return won't be locked properly.

      J 1 Reply Last reply 7 Jul 2017, 04:13
      1
      • C corruptedsyntax
        6 Jul 2017, 14:09

        @jsulm

        QByteArray CAgilentLan::GetLastResponse(void)
        {
            QMutexLocker(m_MtxResponse);
            return m_ByteArrayRead;
        }
        

        Won't that temporary QMutexLocker need a name? Otherwise it will be deconstructed as a temporary object at the end of the first statement and the return won't be locked properly.

        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 7 Jul 2017, 04:13 last edited by
        #6

        @corruptedsyntax Sure, thanks for pointing that out :-)

        QByteArray CAgilentLan::GetLastResponse(void)
        {
            QMutexLocker mutexLocker(m_MtxResponse);
            return m_ByteArrayRead;
        }
        

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        G 1 Reply Last reply 7 Jul 2017, 05:29
        0
        • J jsulm
          7 Jul 2017, 04:13

          @corruptedsyntax Sure, thanks for pointing that out :-)

          QByteArray CAgilentLan::GetLastResponse(void)
          {
              QMutexLocker mutexLocker(m_MtxResponse);
              return m_ByteArrayRead;
          }
          
          G Offline
          G Offline
          Galilio
          wrote on 7 Jul 2017, 05:29 last edited by
          #7

          @jsulm
          Hi,
          So is correct:

          QByteArray CAgilentLan::GetLastResponse(void)
          {
              QMutexLocker mutexLocker(&m_MtxResponse);
              return m_ByteArrayRead;
          }
          
          J 1 Reply Last reply 7 Jul 2017, 05:31
          0
          • G Galilio
            7 Jul 2017, 05:29

            @jsulm
            Hi,
            So is correct:

            QByteArray CAgilentLan::GetLastResponse(void)
            {
                QMutexLocker mutexLocker(&m_MtxResponse);
                return m_ByteArrayRead;
            }
            
            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 7 Jul 2017, 05:31 last edited by
            #8

            @Galilio yes

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            G 1 Reply Last reply 7 Jul 2017, 06:50
            0
            • G Offline
              G Offline
              Galilio
              wrote on 7 Jul 2017, 05:33 last edited by
              #9

              Hi,
              How can I avoid this error?

              1 Reply Last reply
              0
              • J jsulm
                7 Jul 2017, 05:31

                @Galilio yes

                G Offline
                G Offline
                Galilio
                wrote on 7 Jul 2017, 06:50 last edited by
                #10

                @jsulm
                Problem occurs when the application is exited.
                The Destructor is then called, which looks like this:

                CAgilentLan::~CAgilentLan()
                {
                	this->Close(GetiTimeOutClose());
                
                	if (this->Execute("DeleteObject", 0, 0, 0) != enRspOK)
                	{
                		throw(QString("Error AgilentLan.CPP/Destructor TcpSocket Delete memory error"));
                	}
                }
                

                And right here is the problem:

                GetTcpSocket()->disconnectFromHost();
                
                J 1 Reply Last reply 7 Jul 2017, 06:53
                0
                • G Galilio
                  7 Jul 2017, 06:50

                  @jsulm
                  Problem occurs when the application is exited.
                  The Destructor is then called, which looks like this:

                  CAgilentLan::~CAgilentLan()
                  {
                  	this->Close(GetiTimeOutClose());
                  
                  	if (this->Execute("DeleteObject", 0, 0, 0) != enRspOK)
                  	{
                  		throw(QString("Error AgilentLan.CPP/Destructor TcpSocket Delete memory error"));
                  	}
                  }
                  

                  And right here is the problem:

                  GetTcpSocket()->disconnectFromHost();
                  
                  J Offline
                  J Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 7 Jul 2017, 06:53 last edited by
                  #11

                  @Galilio What error? What problem? Can you be more precise?
                  You should not throw exceptions from destructors.

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  G 1 Reply Last reply 7 Jul 2017, 07:04
                  0
                  • J jsulm
                    7 Jul 2017, 06:53

                    @Galilio What error? What problem? Can you be more precise?
                    You should not throw exceptions from destructors.

                    G Offline
                    G Offline
                    Galilio
                    wrote on 7 Jul 2017, 07:04 last edited by Galilio 7 Jul 2017, 07:04
                    #12

                    @jsulm
                    The TcpSocket connection can not be closed
                    Error is:

                    ASSERT failure in QCoreApplication::sendEvent: "Cannot send events to objects owned by a different thread. Current thread dc02d0. Receiver '' (of type 'QNativeSocketEngine') was created in thread 4912418", file kernel\qcoreapplication.cpp, line 541
                    
                    
                    J 1 Reply Last reply 7 Jul 2017, 07:22
                    0
                    • G Galilio
                      7 Jul 2017, 07:04

                      @jsulm
                      The TcpSocket connection can not be closed
                      Error is:

                      ASSERT failure in QCoreApplication::sendEvent: "Cannot send events to objects owned by a different thread. Current thread dc02d0. Receiver '' (of type 'QNativeSocketEngine') was created in thread 4912418", file kernel\qcoreapplication.cpp, line 541
                      
                      
                      J Offline
                      J Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on 7 Jul 2017, 07:22 last edited by
                      #13

                      @Galilio You are doing something wrong with your threads

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      G 1 Reply Last reply 7 Jul 2017, 08:41
                      0
                      • J jsulm
                        7 Jul 2017, 07:22

                        @Galilio You are doing something wrong with your threads

                        G Offline
                        G Offline
                        Galilio
                        wrote on 7 Jul 2017, 08:41 last edited by
                        #14

                        @jsulm
                        Yes but what?
                        My QThread looks like this:

                        m_pvWorkerThread = new QThread();
                        m_pvAgilentWorker = new CAgilentLan(settings_l.strfuGetIP_DSO(), 500, settings_l.bofuGetSimulate());
                        m_pvAgilentWorker->moveToThread(m_pvWorkerThread);
                        connect(m_pvWorkerThread, &QThread::started, m_pvAgilentWorker, &CAgilentLan::agilentStart);
                           
                        connect(m_pvWorkerThread, &QThread::finished, m_pvWorkerThread, &QThread::deleteLater);
                        connect(m_pvWorkerThread, &QThread::finished, m_pvWorkerThread, &CAgilentLan::deleteLater);
                        m_pvWorkerThread->start();
                        
                        J 1 Reply Last reply 7 Jul 2017, 08:44
                        0
                        • G Galilio
                          7 Jul 2017, 08:41

                          @jsulm
                          Yes but what?
                          My QThread looks like this:

                          m_pvWorkerThread = new QThread();
                          m_pvAgilentWorker = new CAgilentLan(settings_l.strfuGetIP_DSO(), 500, settings_l.bofuGetSimulate());
                          m_pvAgilentWorker->moveToThread(m_pvWorkerThread);
                          connect(m_pvWorkerThread, &QThread::started, m_pvAgilentWorker, &CAgilentLan::agilentStart);
                             
                          connect(m_pvWorkerThread, &QThread::finished, m_pvWorkerThread, &QThread::deleteLater);
                          connect(m_pvWorkerThread, &QThread::finished, m_pvWorkerThread, &CAgilentLan::deleteLater);
                          m_pvWorkerThread->start();
                          
                          J Offline
                          J Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on 7 Jul 2017, 08:44 last edited by jsulm 7 Jul 2017, 08:47
                          #15

                          @Galilio Sorry, currently I have no time to analyse your code.
                          You should read: https://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/

                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          0

                          13/15

                          7 Jul 2017, 07:22

                          • Login

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