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. how do we check the object's thread on which it is living

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

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 6.0k 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.
  • R Offline
    R Offline
    RAJEESHR
    wrote on last edited by
    #1

    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

    jsulmJ 1 Reply Last reply
    0
    • R RAJEESHR

      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

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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

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

      1 Reply Last reply
      3
      • B Offline
        B Offline
        Bonnie
        wrote on last edited by Bonnie
        #3

        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.

        1 Reply Last reply
        4
        • R Offline
          R Offline
          RAJEESHR
          wrote on last edited by RAJEESHR
          #4

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

          Thanks @Bonnie and @jsulm for your help.

          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