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. QThread question - QThread::currentThread() gives 3 different addresses
Qt 6.11 is out! See what's new in the release blog

QThread question - QThread::currentThread() gives 3 different addresses

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 10.7k Views 1 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.
  • N Offline
    N Offline
    nando76
    wrote on last edited by
    #1

    Hi,
    i am a little bit confused using threads.

    In my code i creataed a AMQTest class which is inherited from QThread.

    I put debug output with the threadID into constructor, run method
    and in a callback method which gets called if a new mesasges has been received (via activemq).

    What i do not understand is, why do i get 3! different addresses when logging the @QThread::currentThread()@ ??

    For example in constructor i get:

    *AMQTest::AMQTest: threadID= QThread(0x756ba0) *

    Then in run() ---> which should be the thread? it get:

    STARTING RUN: threadID= QThread(0x7fffbdc3acc8, name = "AMTTest - MAIN")

    and last but not least in my callback i get:

    AMQTest message: threadID= QThread(0x7f3298000d90) nreceivedMessages= 1

    This are 3 different addresses for @QThread::currentThread()@

    0x756ba0

    0x7fffbdc3acc8

    0x7f3298000d90

    Could somebody please explain me what happens here?

    Here is a piece of demo code:

    @AMQTest::AMQTest() :
    m_brokerURI("tcp://192.168.0.10:61616"),
    m_destURI("lasttest"),
    m_clientAck(false)
    {
    qDebug() << "=================================================";
    qDebug() << "AMQTest::AMQTest: threadID=" << QThread::currentThread();
    qDebug() << "=================================================";

    QThread::start();
    
    qDebug() << "=================================================";
    qDebug() << "BYE BYE!";
    qDebug() << "=================================================";
    

    }

    AMQTest::~AMQTest()
    {
    qDebug() << "~AMQTest threadID=" << QThread::currentThread();

    qDebug() &lt;&lt; "~AMQTest: Shutting down thread... threadID=" &lt;&lt; QThread::currentThread();
    QThread::wait();
    qDebug() &lt;&lt; "~AMQTest: Thread shutdown completede! threadID=" &lt;&lt; QThread::currentThread();
    

    }

    bool AMQTest::initialize()
    {
    qDebug() << "initialize: threadID=" << QThread::currentThread();
    qDebug() << "brokerURI = " << m_brokerURI;
    qDebug() << "destURI = " << m_destURI;

    try
    {
        activemq::core::ActiveMQConnectionFactory *connectionFactory =
                new activemq::core::ActiveMQConnectionFactory(m_brokerURI.toStdString());
    
        m_cmsConnection = connectionFactory->createConnection();
        qDebug() << "created connection from connection factory";
    
        delete connectionFactory;
    
        activemq::core::ActiveMQConnection *connection =
               dynamic_cast<activemq::core::ActiveMQConnection *>(m_cmsConnection);
    
        connection->start();
        //connection->setExceptionListener(this);
    
        qDebug() << "succsessdully connected to broker: " << m_brokerURI;
    
        m_clientAck ?
               m_session = m_cmsConnection->createSession(cms::Session::CLIENT_ACKNOWLEDGE) :
               m_session = m_cmsConnection->createSession(cms::Session::AUTO_ACKNOWLEDGE);
        m_useTopic ?
               m_destination = m_session->createTopic(m_destURI.toStdString()) :
               m_destination = m_session->createQueue(m_destURI.toStdString());
    
        m_consumer = m_session->createConsumer(m_destination);
        m_consumer->setMessageListener(this);
    }
    catch (cms::CMSException& e)
    {
        printException(e);
        return false;
    }
    
    return true;
    

    }

    void AMQTest::run()
    {
    qDebug() << "STARTING RUN: threadID=" << QThread::currentThread();

    if(!initialize())
        return;
    
    exec&#40;&#41;;
    
    qDebug(&#41; &lt;&lt; "LEAVING  RUN:  threadID=" &lt;&lt; QThread::currentThread(&#41;;
    
    qApp-&gt;quit(&#41;;
    

    }

    void AMQTest::onMessage(const cms::Message *message) throw ()
    {
    static qlonglong received = 0;

    ++received;
    
    if(received &gt; MAX_MESSAGES)
    {
        qDebug() &lt;&lt; "max messages: " << MAX_MESSAGES << " received -> stopping thread";
        QThread::exit(1);
        return;
    }
    
    qDebug() << "AMQTest message:  threadID=" &lt;&lt; QThread::currentThread() &lt;&lt; " nreceivedMessages=" &lt;&lt; received;
    
    
    //const cms::TextMessage *textMsg = dynamic_cast&lt;const cms::TextMessage *&gt;(message);
    
    //qDebug() &lt;&lt; textMsg-&gt;getText().c_str();
    

    }

    void AMQTest::printException(const cms::CMSException &e)
    {
    qDebug() << "======== ActiveMQ Exception ========";
    qDebug() << QString::fromStdString(e.getStackTraceString());
    qDebug() << QString::fromStdString(e.what());
    qDebug() << QString::fromStdString(e.getMessage());
    qDebug() << "=====================================";
    }@

    Thank you

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      The thread of the constructor is the thread where you instantiate your object

      The thread in the run function, is your QThread's managed thread.

      The thread of the callback is the thread that is calling the callback function

      Hope it helps

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • JKSHJ Offline
        JKSHJ Offline
        JKSH
        Moderators
        wrote on last edited by
        #3

        Hi,

        QThread::currentThread() is a static function. Its return value depends on the thread that calls it.

        QThread is not a thread. It is an object that manages a thread. You can say that a QThread straddles 2 threads:

        • The QThread object is constructed in one thread, so it lives in this thread, but
        • Its run() function executes in a different thread

        [quote]

        0×756ba0

        0×7fffbdc3acc8

        0×7f3298000d90

        [/quote]Pointer #1 is very far away from pointers #2 and #3. I'm not sure if it is valid.

        Questions:

        Where did you construct your AMQTest?

        What is onMessage() connected to, and how is it connected?

        Do you have any other threads in your program?

        By the way, your AMQTest class does lots of processing. Thus, it should be a subclass of QObject, not QThread. This is especially important since you want to use a slot (onMessage()) -- QThread subclasses should never use new slots to do processing.

        Quoting from the "QThread documentation":http://qt-project.org/doc/qt-5.1/qtcore/qthread.html:

        "It is important to remember that a QThread object usually lives in the thread where it was created, not in the thread that it manages. This oft-overlooked detail means that a QThread's slots will be executed in the context of its home thread, not in the context of the thread it is managing. For this reason, implementing new slots in a QThread subclass is error-prone and discouraged."

        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

        1 Reply Last reply
        0
        • JKSHJ Offline
          JKSHJ Offline
          JKSH
          Moderators
          wrote on last edited by
          #4

          I also highly recommend reading this page for an overview on different ways to use threads in Qt: http://doc-snapshot.qt-project.org/qt5-stable/threads-technologies.html

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          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