Congratulations to our 2022 Qt Champions!

The same value of different threads ids?

  • Re: why threads id are same?

    I Have a class RECEIVER that have a member variables m_thread and m_rxUdp.
    The following is the instantiation of that class in main function:
    RECEIVER rx(QHostAddress::LocalHost, 4012, QHostAddress::LocalHost, 3500);

    Before instantiation of that object, i have tried the following:
    qDebug()<<"Main Function Thread ID = "<<QThread::currentThreadId();
        qDebug()<<"QCoreApplication ::instance()->thread() = "<<QCoreApplication ::instance()->thread();

    It gave me:
    Main Thread ID = 4420
    QCoreApplication ::instance()->thread() = QThread(0x8377838)

    respectively. And inside the receiver thread i have started the m_thread and tested that the 2 threads are working at the same time (this was done by doing while loop in the main function and at the same time trying to receive data from udp) and it worked well, but when i tried to use

    qDebug()<<"Main Function Thread ID = "<<QThread::currentThreadId();

    inside the receiver constructor and after the ```
    this->moveToThread(m_thread); //where this refers to the receiver class

    was called, it gave me the same number as the main function numbers.
    My questions are:
    1. What is the difference between the return of the following 2 functions  :
    qDebug()<<"Main Thread ID = "<<(int)QThread::currentThreadId();
        qDebug()<<"QCoreApplication ::instance()->thread() = "<<QCoreApplication ::instance()->thread();

    2.Why the return of ```
    qDebug()<<"Main Thread ID = "<<(int)QThread::currentThreadId();

    from the 2 threads are the same?

  • Lifetime Qt Champion


    1. - it returns pointer to the QThread instance
      QCoreApplication ::instance()->thread() returns the QThread instance managing the thread where QCoreApplication instance is living
    2. I guess you did not start second thread
      It is unclear how you actually start second thread.

  • I wrote this code inside the receiver constructor:

    1. Why the QCoreApplication ::instance()->thread() is not the same as the main function id thread?
    m_rxUdp = new QUdpSocket(this);
    m_thread = new QThread;
    bool binded = m_rxUdp->bind(m_listeningAddress, m_listeningPort);
    qDebug()<<"New Thread ID = "<<(int)QThread::currentThreadId();

    Also i have tested the performance of the 2 threads, and i am sure it was working. but the question is why the thread id is the same?

  • @Ahmed000001
    I'm confused about some of what you say/are asking. But I think you need to know:

    Qt seems a bit awkward about giving you thread ids. It seems the only way you can access these is by calling the (static) function QThread::currentThreadId(). This returns the id of the currently executing thread, i.e. the thread from which that (static) function is being called. There doesn't seem to be a way of getting the thread id from a QThread instance, from what I can see :( [If a Qt expert knows better I'd like to hear, but this is what I gather from my investigations, even though it seems odd to me....]

    So, going back to your question.... If you think

    qDebug()<<"New Thread ID = "<<(int)QThread::currentThreadId();

    is going to report the id of m_thread simply because you start()ed it, it is not. The thread where that line is called is still the parent/UI thread, and that's what it should it report.

    You need to call QThread::currentThreadId(); somewhere in the m_thread code, after you have called start().

    Am I right?

  • Yes you are right, i have tested what you said and tried to get the id from inside a function that was executed after the thread has started and it gave me the id of the new thread, Thank you for your time.

Log in to reply