Results 1 to 10 of 10

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

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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

  2. #2
    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...

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

    Artschi (30th May 2006)

  4. #3
    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.

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

    Artschi (30th May 2006)

  6. #4
    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.

  7. #5
    Join Date
    Oct 2017
    Posts
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

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


  8. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,315
    Thanks
    314
    Thanked 870 Times in 857 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, 21: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, 21:55
  3. Is it possible to create a QThread without inheriting ?
    By probine in forum Qt Programming
    Replies: 6
    Last Post: 23rd March 2006, 22:51
  4. QProcess in a QThread
    By chombium in forum Qt Programming
    Replies: 2
    Last Post: 11th January 2006, 15:52
  5. Replies: 2
    Last Post: 6th January 2006, 21: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
  •  
Qt is a trademark of The Qt Company.