1

I have a multi-threaded embedded architecture that contains 6 application specific processes which are executed when the initialization process is executed. Likewise, each have their own number of threads that are running.

What i want to do is suspend the running threads of 1 particular process based on whether the device is connected to the pc or not.

I have tried searching around and the closest i've found to what im looking for is the following: How to obtain list of thread handles from a win32 process?

However, that code returns the list of all running threads. This wont work for me since im trying to suspend all obtained threads, assuming they have been obtained from the same process, thus i do not check which process they belong too.

Likewise, i am obtaining the list of running threads of a processes in another process.

Is there an existing method from windows that allows such control, or am i stuck with having to identify which threads i need to suspend from the entire list?

Community
  • 1
  • 1
Javia1492
  • 862
  • 11
  • 28
  • 1
    *What i want to do is suspend the running threads.* No that's the last thing that you want to do. Read the documentation for `SuspendThread`. Specifically *This function is primarily designed for use by debuggers. It is not intended to be used for thread synchronization. Calling SuspendThread on a thread that owns a synchronization object, such as a mutex or critical section, can lead to a deadlock if the calling thread tries to obtain a synchronization object owned by a suspended thread.* Signal the thread to suspend itself. – David Heffernan Jun 09 '15 at 22:03
  • @DavidHeffernan How is signaling the thread to suspend itself any different than suspending it using `SuspendThread`? Im not quite sure about your meaning of "signal a thread". – Javia1492 Jun 11 '15 at 14:13
  • You signal the thread with an event. The thread regularly checks the event. When it is set, it waits until the event is unset. But it has to be done from the inside so that the thread suspends itself when it is safe to do so. Only the thread can know that. – David Heffernan Jun 11 '15 at 14:15
  • @DavidHeffernan The issue with this method is that i am trying to suspend the thread of process 1 from process 2. – Javia1492 Jun 11 '15 at 14:17
  • That's fine. Events can be shared between processes. Anyway, you've got no choice in the matter. You cannot reliably suspend a thread from the outside. You'll need to accept that and then move on to a cooperative solution. – David Heffernan Jun 11 '15 at 14:19
  • @DavidHeffernan Ok, i was not sure of whether or not i need to be monitoring the status of the thread from within the thread or if i can do it externally, but you say i can do it externally. Thanks for the help. – Javia1492 Jun 11 '15 at 14:20
  • Well, I don't think you monitor the status of the thread at all. You just set the event. If you need to know that the thread has responded then you need it to signal that. – David Heffernan Jun 11 '15 at 14:22
  • I have edited my answer to cover the additional questions. – Codeguard Jun 11 '15 at 14:41

1 Answers1

4

Instead of trying to forcefully suspend threads (which is likely to bring you trouble when you suspend in "not so lucky moment") you'd rather use a named CreateEvent() with manual reset.

  • Named events are easily shared between processes. You simply CreateEvent() again with the same name. The typical name for event would be MyCompany_MyProduct_MyFeature_EventName to prevent accidental collisions.
  • When you WaitForSingleObject() on "set" event, the wait is immediately satisfied.
  • When you wait on "reset" event, the wait suspends your thread until event is set.
  • Your first application will have its thread(s) wait on event when they're not doing any work and therefore safe to suspend.
  • You will set and reset event from second application to control the first application.
  • This way, you don't need to enumerate threads, and it's more robust.
Codeguard
  • 7,787
  • 2
  • 38
  • 41