Can I get a threads suspend count under Windows CE, using C or Visual C++, without calling resume or suspend functions? The only way I can see of doing it is something like
int Count = SuspendThread(ThreadHandle);
ResumeThread(ThreadHandle);
This has a couple of problems, firstly, I'd rather not suspend the thread, and secondly the suspend might fail if the thread is running kernel code. I can work around this, but I feel there should be a more elegant solution. I could also reverse it using
int Count = ResumeThread(ThreadHandle);
SuspendThread(ThreadHandle);
But this has similar problems. Any good alternative method of getting the suspend count from the handle?
I have a combined solution. Use WaitForSingleObject() to determine if the thread is suspended or not.
If it's not suspended, the suspend count is obviously 0.
If it's suspended, it's safe to call SuspendThread() to get the suspend count. Since it's already suspended you will not stall anything.
You should not suspend any thread on any platform, ever.
You should instead add synchronization points in your threading code that explicitly waits for a flag to become signaled before it is allowed to continue. This way you know where it will be paused, or at least know that it will be paused at safe points.
The following operations on threads should be banned, outright, from any platform for any programmer:
You should never, ever, forcibly impose your will from the outside on a thread. You have no guarantee what it is doing, what kind of resources it is currently locking.
Always write threading in a cooperative mode. Your thread should be aware of its surroundings, and yield to wishes of the outside world to either exit in an orderly fashion, or pause until it can safely continue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With