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. The same value of different threads ids?

The same value of different threads ids?

Scheduled Pinned Locked Moved Solved General and Desktop
qthreadscurrent
5 Posts 3 Posters 3.3k 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.
  • A Offline
    A Offline
    Ahmed000001
    wrote on last edited by Ahmed000001
    #1

    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
    m_thread->start();

    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?
    jsulmJ 1 Reply Last reply
    0
    • A Ahmed000001

      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
      m_thread->start();

      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?
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Ahmed000001

      1. http://doc.qt.io/qt-5/qobject.html#thread - 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.

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

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Ahmed000001
        wrote on last edited by Ahmed000001
        #3

        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);
        this->moveToThread(m_thread);
        m_thread->start();
        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?

        JonBJ 1 Reply Last reply
        0
        • A Ahmed000001

          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);
          this->moveToThread(m_thread);
          m_thread->start();
          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?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @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

          m_thread->start();
          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?

          1 Reply Last reply
          3
          • A Offline
            A Offline
            Ahmed000001
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            2

            • Login

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