Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

how do we check the object's thread on which it is living



  • for instance i have a QObject pointer created one of the thread in my application and consumed in another thread.
    I wanted to know which thread owns that object.
    is there any API for that?
    QObject::thread()::currentThreadId() - is returning calling thread id(from where we are invoking ) only.

    eg:
    Thread 1
    {
    QObject* object = new QObject();
    }
    Thread 2
    {
    /// Below line is not the one i want
    object->thread()->currentThreadId(); /// Returns Thread 2's id only
    /// I want a solution something like below line
    object->thread()->threadId() /// Returns Thread 1's id.
    }
    Anyone plz help me.

    Thanks in advance,
    Raj


  • Qt Champions 2019

    @RAJEESHR said in how do we check the object's thread on which it is living:

    I wanted to know which thread owns that object.

    Either the thread where the object was created or the thread to which the object was moved using https://doc.qt.io/qt-5/qobject.html#moveToThread

    To find out in which thread an object lives use https://doc.qt.io/qt-5/qobject.html#thread



  • currentThreadId() is a static funtion. It has nothing to do with the object or its living thread...
    object->thread()->currentThreadId() equals QThread::currentThreadId().
    Also the doc says:

    Warning: The handle returned by this function is used for internal purposes and should not be used in any application code.

    So better not to use this function.

    For "which thread owns that object", you already write that, it is just object->thread().
    Don't know why you must get a thread id...But QThread seems not providing an API for that.
    If you want to compare, just compare the pointer address.



  • Ok. understood. I will compare QThread* instead of thread id.

    Thanks @Bonnie and @jsulm for your help.


Log in to reply