The same value of different threads ids?
-
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 useqDebug()<<"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?
-
- 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 - I guess you did not start second thread
It is unclear how you actually start second thread.
- http://doc.qt.io/qt-5/qobject.html#thread - it returns pointer to the QThread instance
-
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?
-
@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
) functionQThread::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 aQThread
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 youstart()
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 them_thread
code, after you have calledstart()
.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.