Click here to Skip to main content
13,096,157 members (50,899 online)
Rate this:
Please Sign up or sign in to vote.
See more:
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! :)
Posted 30-Mar-13 18:00pm

1 solution

Rate this: bad
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:[^].

Your thread to be suspended/resumed should call WaitForSingleObject on the event instance:[^].

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.

CPallini 31-Mar-13 2:20am
Very good, my 5.
Sergey Alexandrovich Kryukov 31-Mar-13 11:26am
Thank you very much, Carlo.
H.Brydon 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 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...

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.170813.1 | Last Updated 31 Mar 2013
Copyright © CodeProject, 1999-2017
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