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
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
Try return QThread::currentThreadId() from actual thread .
a life without programming is like an empty bottle
QThread::currentThreadId() is static and relates to the actual thread but NOT to the thread described by a given QThread object.
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
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 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.
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.Originally Posted by iGoo
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