Reputation: 22932
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?
Upvotes: 7
Views: 3401
Reputation: 391664
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.
Upvotes: 1
Reputation: 581
Even the Thread in Active you will still receive a WAIT_TIMEOUT result, this because Threads only signal when they finish, not when they're running.
That said WaitForSingleObject(hThread,INFINITE) waits until the threads finishes.
Upvotes: 0
Reputation: 133188
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.
Upvotes: 4
Reputation: 43150
Probably with WaitForSingleObject you can check if the thread is suspended but you can not retrieve the suspend counter.
Upvotes: 0