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.
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
Maybe, I misunsterstood what you want but... Since the manual tells:
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..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.
Ok, maybe everybody already thought about this but I had to try...![]()
Artschi (30th May 2006)
Maybe, I misunsterstood what you want but... Since the manual tells:
Quote:
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..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.
Ok, maybe everybody already thought about this but I had to tryIf so,it behaves as windows apiOn Windows, the returned value is a pseudo-handle for the current thread that cannot be used for numerical comparison.
HANDLE GetCurrentThread(VOID);
you can then call DuplicateHandle to thanslate it to real id.
refer MSDN for dedails.
Artschi (30th May 2006)
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 confusedI 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.
I would like not to descent to such platform specific features in application code. Actually, this is one (good) reason to use QTOriginally Posted by iGoo
But, Thanks anyway.
correct answer here:
proper-use-of-qthread-currentthreadid
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.
Bookmarks