0

I am suspending a thread using an event. When the eventSuspend is signaled the thread waits on it.

// inside the thread function
WaitForSingleObject(eventSuspend, INFINITE );

Now from outside I set it to wait by eventSuspend.ResetEvent() but loop inside the thread function is kind of long (time consuming). How can I know that the thread has finished whatever it was doing and now it is indeed waiting on this signal?

zar
  • 11,361
  • 14
  • 96
  • 178
  • 2
    Just use another event. – Hans Passant Oct 25 '13 at 21:24
  • @HansPassant events are kernal objects and kind of expensive (sluggish), I was wondering if I can use the same event somehow but that doesn't make sense..does it :) oh I think might by using auto reset event – zar Oct 25 '13 at 21:26
  • The overhead of an additional event object is negligible. – Harry Johnston Oct 26 '13 at 01:24
  • Open up Taskmanager and look at the Handle count. You should see a figure somewhere between 10k and 20k. Do you really think that **one** event object is *kind of expensive*? – IInspectable Oct 27 '13 at 01:35
  • @IInspectable what you say make sense. I guess the reason I was vary of it was because of the fact that MFC library is not thread safe to boost performance and I read in a multithreading book that using kernal objects are expensive (time wise). – zar Oct 28 '13 at 13:56
  • Just to let another thread know that a specific state is reached, there is no need for an event. A simple integer or bool might be sufficient, **when you don't need to wait on it!!!** – xMRi Oct 29 '13 at 08:38
  • @xMRi A simple integer or bool is not good. How will you check the state..in a loop? At least event lets you check it cleanly. – zar Oct 29 '13 at 13:39
  • As I wrote: If you don't have to wait on it, and just want to know if a specific state is reached any variable (volatile) is OK. – xMRi Oct 29 '13 at 14:00

1 Answers1

0

Your question is wrong: A thread that is waiting is not suspended!

A thread is suspended after calling SuspendThread() but not after calling WaitForSingleObject().

Simply set a BOOL flag when the thread loop starts and reset the flag when the thread loop exits then you know if it runs or not.

If you want futher informations about a thread, like if it is supended or not, you can use the code that I posted here: How to get thread state (e.g. suspended), memory + CPU usage, start time, priority, etc

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