How to determine that a win32 thread is either in Wait or Join or Sleep state in c++
Asked Answered
E

4

3

What I actually search for is c++/win32 equivalent for .net ThreadState Enumeration.

Any suggestions?

Elagabalus answered 18/11, 2011 at 16:15 Comment(5)
NtQuerySystemInformation() but it isn't documented. Only documented way is WMI with the Win32_Thread class.Currin
Thank you all for you answers I really do appreciate your effort!!! However, I am looking for something like BOOL IsThreadInWaitJoinSleep(HANDLE threadHandle)? Any other suggestions?Elagabalus
You can't get that. Same kind of reason that you can't find out that a file is locked without actually trying to open it. Because such an operation is hopelessly unusable on a multitasking operating system. A nanosecond later the file might be locked. Or the thread might be blocked.Currin
@Hans Passant: How come then that I can get this information for a .net thread?Elagabalus
You can't, there's no way to map a ProcessThread to a Thread. Strictly informational only.Currin
M
3

There's very little difference between these, all are waits for different kernel objects.

By "Wait" I assume you mean "I/O wait". "Join" is just "wait for a thread/process". And "Sleep" is "wait for a timer".

To complicate matters more, a thread can be waiting for some combination of kernel objects.

You could find out what objects a thread is waiting for, and the type of those objects, using a kernel debugger. I don't think there's any simpler way.

Myxomatosis answered 18/11, 2011 at 16:18 Comment(0)
S
2

There is no direct equivalent - managed and unmanaged threads should not be considered the same. See here.

An operating-system ThreadId has no fixed relationship to a managed thread, because an unmanaged host can control the relationship between managed and unmanaged threads. Specifically, a sophisticated host can use the Fiber API to schedule many managed threads against the same operating system thread, or to move a managed thread among different operating system threads.

Soundless answered 18/11, 2011 at 16:25 Comment(0)
P
1

The only state of a native thread easily accesible using the winapi is for knowning if the thread has finished its execution. Just use the function WaitForSingleObject() with the thread handle and a timeout of 0:

DWORD res = WaitForSingleObject(handleThread, 0);
switch (res)
{
    case WAIT_OBJECT_0:
        printf("The thread has finished\n");
        break;
    case WAIT_TIMEOUT:
        printf("The thread is still running\n");
        break;
    default:
        printf("Unknown error (shouldn't happen)\n");
        break;

}
Plasty answered 18/11, 2011 at 22:34 Comment(0)
B
1

The "problem" is that the .NET run time has ownership of your .NET thread. So when you call Abort for example, the run time internals throws a ThreadAbortException exception in the .NET context of your .NET thread, and that's how you can catch it in your thread using catch(ThreadAbortException).

And the same is true with ThreadState, since it has the underlying ownership of your thread, it knows exactly what it's doing and therefore can produce a valid thread state.

Since there is no non-hackins way to officially to query a thread for it's internal state, you could wrap this up in a class. But again, you would entirely depend upon the thread method to adhere to any .Abort()-requests.

Babettebabeuf answered 20/9, 2012 at 6:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.