Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ Windows
Hi, first thank for read.
I'm having trouble with SuspendThread of Kernel32.dll and I can't solve because I'm using multithead and when I suspend my process(hooked the .dll) and later I kill all Threads my protection stop of work.
Any ideas? or help to create a Driver or Service?
Thanks! Smile | :)
Posted 30-Mar-13 19:00pm

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Using SuspendThread is a known unsafe technique; for example, this is the reason why the corresponding API in .NET is marked obsolete.
 
The right approach is this: you should use Windows API event object:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms686915%28v=vs.85%29.aspx[^].
 
Your thread to be suspended/resumed should call WaitForSingleObject on the event instance:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms687032(v=vs.85).aspx[^].
 
When the event object is non-signalled, the thread will be put in the wait state; it will be switched off an not scheduled to execution, thus wasting zero CPU time, until it is awaken. One of the way to awake it is to put the same event object in a signaled state, which you can do in another thread. This way, a thread can be throttled by other threads.
 
As a thread can be put to wait state only in few points specially selected by the developer, such code can be written in a safe way, in contrast to SuspendThread which is called asynchronously and can suspend a thread at a "bad" moment of time. A typical example of the disaster: a thread took a mutex object (or critical section; any of the objects used for mutual exclusion) and was suspended before releasing it; which can create a random (worse) kind of deadlock, because some other threads use the same mutex object.
 
—SA
  Permalink  
v5
Comments
CPallini at 31-Mar-13 2:20am
   
Very good, my 5.
Sergey Alexandrovich Kryukov at 31-Mar-13 11:26am
   
Thank you very much, Carlo.
—SA
H.Brydon at 31-Mar-13 15:37pm
   
Another way to say it is that a given thread should be set up to suspend activity on itself (rather than doing it externally), and the coding strategy should be that when you want to suspend/pause activity on a thread, a message of some sort is sent to the thread, which takes the desired 'suspend' action on itself when it notices the message. This provides a mechanism to avoid problems with deadlocks, files left open, resources allocated inappropriately etc.
Sergey Alexandrovich Kryukov at 31-Mar-13 21:01pm
   
Thank you.
This is not exactly "on itself". This is done in collaboration and with synchronization. A good list of problems...
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web03 | 2.8.1411022.1 | Last Updated 31 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100