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
Forum Updated to NodeBB v4.3 + New Features

Use Qt thread correctly

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 3 Posters 3.2k 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 corruptedsyntax

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

    jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on 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
    0
    • jsulmJ jsulm

      @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 last edited by
      #7

      @jsulm
      Hi,
      So is correct:

      QByteArray CAgilentLan::GetLastResponse(void)
      {
          QMutexLocker mutexLocker(&m_MtxResponse);
          return m_ByteArrayRead;
      }
      
      jsulmJ 1 Reply Last reply
      0
      • G Galilio

        @jsulm
        Hi,
        So is correct:

        QByteArray CAgilentLan::GetLastResponse(void)
        {
            QMutexLocker mutexLocker(&m_MtxResponse);
            return m_ByteArrayRead;
        }
        
        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #8

        @Galilio yes

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

        G 1 Reply Last reply
        0
        • G Offline
          G Offline
          Galilio
          wrote on last edited by
          #9

          Hi,
          How can I avoid this error?

          1 Reply Last reply
          0
          • jsulmJ jsulm

            @Galilio yes

            G Offline
            G Offline
            Galilio
            wrote on 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();
            
            jsulmJ 1 Reply Last reply
            0
            • G Galilio

              @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();
              
              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on 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
              0
              • jsulmJ jsulm

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

                G Offline
                G Offline
                Galilio
                wrote on last edited by Galilio
                #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
                
                
                jsulmJ 1 Reply Last reply
                0
                • G Galilio

                  @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
                  
                  
                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 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
                  0
                  • jsulmJ jsulm

                    @Galilio You are doing something wrong with your threads

                    G Offline
                    G Offline
                    Galilio
                    wrote on 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();
                    
                    jsulmJ 1 Reply Last reply
                    0
                    • G Galilio

                      @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();
                      
                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by jsulm
                      #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

                      • Login

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