17

How do I check to see if a Win32 thread is running or in suspended state?

I can't find any Win32 API which gives the state of a thread. So how do I get the thread state?

random
  • 9,774
  • 10
  • 66
  • 83
Canopus
  • 7,351
  • 11
  • 46
  • 57

7 Answers7

14

I think - originally - this information was not provided because any API that provided this info would be misleading and useless.

Consider two possible cases - the current thread has suspended the thread-of-interest. Code in the current thread knows about the suspended state and should be able to share it so theres no need for the kernel team to add an API.

The 2nd case, some other / a 3rd thread in the system has suspended the thread of interest (and theres no way to track which thread that was). Now you have a race condition - that other thread could, at any time - unsuspend the thread of interest and the information gleaned from the API is useless - you have a value indicating the thread is suspended when it is in fact, not.

Moral of the story - if you want to know that a thread is suspended - suspend it: The return value from SuspendThread is the previous suspend count of the thread. And now you DO know something useful - The thread WAS AND STILL IS suspended - which is useful. Or that it WASN't (but now is) suspended. Either way, the thread's state is now deterministically known so you can in theory make some intelligent choices based on that - whether to ResumeThread, or keep it suspended.

Chris Becke
  • 34,244
  • 12
  • 79
  • 148
  • 1
    Yep - any attempt to query the thread suspend state is going to be inherently racy – Ana Betts Jun 17 '09 at 16:10
  • 7
    There are thread states beyond "suspended" and "running." I think it's more likely that the OP is interested in knowing which threads are blocked. In any case, your point about race conditions is a good one. If there is a way to get a thread's state, it should be used only for informational purposes. Any attempt to use it for control flow will lead to a world of hurt. – Peter Ruderman Jun 17 '09 at 17:49
  • Exactly - suspended or not suspended is typically not of interest. Running or waiting on a synchronisation signal.... that is typically what is of interest, and is clearly something that can be determined since debuggers and tools such as Performance Monitor surface precisely this information. Furthermore in a properly written application, threads will *never* be suspended unless running under a debugger. – Deltics Nov 23 '09 at 21:45
  • But what if you actually enum threads with a snapshot and you suspend the main thread or the thread that is enuming them? – Ben Feb 03 '14 at 06:42
  • 3
    This answer is WRONG. Why does it have 9 upvotes?? You find a working code here: http://stackoverflow.com/questions/22949725/how-to-get-thread-state-e-g-suspended-memory-cpu-usage-start-time-priori – Elmue Apr 08 '14 at 22:57
  • This is such a typical SO answer. OP asks a question and the top answer is "it is impossible" because the person answering believes it is wrong to do that. – Ramon Apr 15 '21 at 01:19
6

You can get this information by calling NtQuerySystemInformation() with the value for SystemProcessesAndThreadsInformation (integer value 5).

If you want an example of what you can do with this information take a look at Thread Status Monitor.

Stephen Kellett
  • 3,078
  • 1
  • 22
  • 25
  • This answer is correct, But the code is missing. It is not easy to use this API. I posted a working class here: http://stackoverflow.com/questions/22949725/how-to-get-thread-state-e-g-suspended-memory-cpu-usage-start-time-priori – Elmue Apr 08 '14 at 23:00
2

WMI's Win32_Thread class has a ThreadState property, where 5: "Suspended Blocked" and 6:Suspended Ready.

You will need the Thread's Id to get the right instance directly (the WMI object's Handle property is the thread id).

EDIT: Given this PowerShell query:

gwmi win32_thread | group ThreadState

gives

Count Name  Group
----- ----  -----
    6 2     {, , , ...}
  966 5     {, , , ...}

WMI has a different definition of "Suspended" to Win32.

Richard
  • 106,783
  • 21
  • 203
  • 265
1

In Windows 7, you can use QueryUmsThreadInformation. (UMS stands for User mode scheduling).

See here for UmsThreadIsSuspended.

Brian R. Bondy
  • 339,232
  • 124
  • 596
  • 636
  • 2
    Minimum client requirement: Windows 7 (64-bit only) – Marco van de Voort Jun 17 '09 at 12:43
  • For pre Windows 7, I wonder what SuspendThread returns for an already suspended thread. Maybe you can check this way by temporarily suspending/resuming and checking the return values. Just an idea, might not work. – Brian R. Bondy Jun 17 '09 at 12:47
1

you could get thread suspend count with code like this:

DWORD GetThreadSuspendCount(HANDLE hThread) {
    DWORD dwSuspendCount = SuspendThread(hThread);
    ResumeThread(hThread);
    return dwSuspendCount;
}

but, as already said - it is not accurate. Moreover, suspending a thread is evil.

Andrey
  • 4,216
  • 1
  • 23
  • 31
  • 3
    A very very bad idea to suspend a thread just to get it's state! What performance will that have if the thread is very busy? – Elmue Apr 08 '14 at 22:59
1

YES: it IS possible to get the thread state and determine if it is suspended.

And NO: You don't need Windows 7 to do that.

I published my working class here on Stackoverflow: How to get thread state (e.g. suspended), memory + CPU usage, start time, priority, etc

This class requires Windows 2000 or higher.

Community
  • 1
  • 1
Elmue
  • 7,602
  • 3
  • 47
  • 57
0

I think the state here is referred to as

  • If thread is in thread proc , doing some processing Or
  • Waiting for event

This can be taken care of by using variable which can tell that if a thread is actually running or waiting for event to happen.

These scenarios appear when considering threadpools, having some n threads and based on each thread running status , tasks can be assigned to idle threads.

anand
  • 11,071
  • 28
  • 101
  • 159