Results 1 to 10 of 10

Thread: How can I get the thread ID out of QThread

  1. #1
    Join Date
    Jan 2006
    Location
    Germany, Rostock
    Posts
    17
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How can I get the thread ID out of QThread

    Hi everybody,

    I'd like to know the tread ID based on a given QThread object. Is there any way to get it?

    Note: The given QThread object doesn't correspond with the actual thread; thus CurrentThreadId() would not work.

    Thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Ukraine,Lviv
    Posts
    454
    Thanks
    9
    Thanked 27 Times in 27 Posts
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: How can I get the thread ID out of QThread

    Try return QThread::currentThreadId() from actual thread .
    a life without programming is like an empty bottle

  3. #3
    Join Date
    Jan 2006
    Location
    Germany, Rostock
    Posts
    17
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How can I get the thread ID out of QThread

    QThread::currentThreadId() is static and relates to the actual thread but NOT to the thread described by a given QThread object.

  4. #4
    Join Date
    Jan 2006
    Location
    Ukraine,Lviv
    Posts
    454
    Thanks
    9
    Thanked 27 Times in 27 Posts
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: How can I get the thread ID out of QThread

    Yea but try subclass QThread and declare some public method like int getId() in it. And then in this method do return QThread::currentThreadId() i think that it must return correct value of thread id...
    But its just INHO i'm not sure if it will be work
    If you want you can check am i right
    a life without programming is like an empty bottle

  5. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How can I get the thread ID out of QThread

    It does not work. If the getId() is called from another thread, QThread::currentThreadId() would naturally return the identifier of the currently executing thread, which is the caller, not the thread whose method was called.
    J-P Nurmi

  6. #6
    Join Date
    Jan 2006
    Location
    La Spezia,Italy
    Posts
    77
    Thanks
    9
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How can I get the thread ID out of QThread

    Maybe, I misunsterstood what you want but... Since the manual tells:
    Warning: The handle returned by this function is used for internal purposes and should not be used in any application code. On Windows, the returned value is a pseudo-handle for the current thread that cannot be used for numerical comparison.
    what about subclassing QThread and pass and int value in the constructor and call THAT value you threadId? After all you can safely use an int getId() function that returns that integer..

    Ok, maybe everybody already thought about this but I had to try...

  7. The following user says thank you to vratojr for this useful post:

    Artschi (30th May 2006)

  8. #7
    Join Date
    May 2006
    Posts
    32
    Thanks
    15
    Thanked 2 Times in 2 Posts

    Cool Re: How can I get the thread ID out of QThread

    Maybe, I misunsterstood what you want but... Since the manual tells:

    Quote:
    Warning: The handle returned by this function is used for internal purposes and should not be used in any application code. On Windows, the returned value is a pseudo-handle for the current thread that cannot be used for numerical comparison.
    what about subclassing QThread and pass and int value in the constructor and call THAT value you threadId? After all you can safely use an int getId() function that returns that integer..

    Ok, maybe everybody already thought about this but I had to try
    On Windows, the returned value is a pseudo-handle for the current thread that cannot be used for numerical comparison.
    If so,it behaves as windows api
    HANDLE GetCurrentThread(VOID);
    you can then call DuplicateHandle to thanslate it to real id.
    refer MSDN for dedails.

  9. The following user says thank you to iGoo for this useful post:

    Artschi (30th May 2006)

  10. #8
    Join Date
    Jan 2006
    Location
    Germany, Rostock
    Posts
    17
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How can I get the thread ID out of QThread

    Ups,

    thanks for this advice. I missed that part in the documentation.

    Sometime it is quite useful to check the manual - even for 'trivial' APIs such as get--Id()

    But, at the end I'm completely confused I realized that the thread ID - as provided by QThread::getThreadId() - is not a unique Id/number that is qualified to identify and manage threads on application level, because:
    • it can't be handed over from one thread context to another
    • it can't be handed over from one thread context to another
    • it doesn't uniquely identify running treads by different threads, which would require a numerical comparison of the ID/Handle.
    • even printing in log statements wouldn't be unique and portable (because of different typedefs).
    • it isn't possible to recreate/get a specific QThread object based on a ID/Handle

    I guess (hope) I'm wrong with this assumption.

    Quote Originally Posted by iGoo
    If so,it behaves as windows api
    HANDLE GetCurrentThread(VOID);
    you can then call DuplicateHandle to thanslate it to real id.
    refer MSDN for dedails.
    I would like not to descent to such platform specific features in application code. Actually, this is one (good) reason to use QT But, Thanks anyway.

  11. #9
    Join Date
    Oct 2017
    Posts
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: How can I get the thread ID out of QThread


  12. #10
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How can I get the thread ID out of QThread

    Finally, after 11 1/2 years, @Artschi has his answer. I mean really, don't you check the dates on posts before replying to them?
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. [QT4] QThread and printing a QList<QPixmap>
    By KShots in forum Qt Programming
    Replies: 3
    Last Post: 24th April 2006, 22:44
  2. Workload in a QThread blocks main application's event loop ?
    By 0xBulbizarre in forum Qt Programming
    Replies: 14
    Last Post: 9th April 2006, 22:55
  3. Is it possible to create a QThread without inheriting ?
    By probine in forum Qt Programming
    Replies: 6
    Last Post: 23rd March 2006, 23:51
  4. QProcess in a QThread
    By chombium in forum Qt Programming
    Replies: 2
    Last Post: 11th January 2006, 16:52
  5. Replies: 2
    Last Post: 6th January 2006, 22:15

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.