Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i know if a thread is suspended under Windows CE

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?

like image 906
SmacL Avatar asked May 22 '09 12:05

SmacL


2 Answers

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.

like image 127
ralphtheninja Avatar answered Sep 17 '22 15:09

ralphtheninja


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:

  • Suspend
  • Resume (since you don't need it if you can't suspend the thread)
  • Kill/Abort

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.

like image 29
Lasse V. Karlsen Avatar answered Sep 20 '22 15:09

Lasse V. Karlsen