Bringing Threads to sleep and waking them

Started by
6 comments, last by LonelyStar 20 years, 5 months ago
Hi, Is there a way to bring a thread to sleep (from inside the thread) and waking them from outside on demand? My Idea would be: void TheThread() { ... ThreadSleeping=true; while(ThreadSleeping) Sleep(0); ... } void OutsideWorld() { ... if(NeedToWakeUpThread) ThreadSleeping=false; ... } But isn't there a less resource-killing way? I am lokking for solutions on Win32 and Linux. Thanks! Nathan [edited by - LonelyStar on November 16, 2003 7:37:59 AM]
Advertisement
AFAIK Semaphore objects are designed for this purpose. Look them up in the Win32 API Reference for more info.
Take a look at SuspendThread and ResumeThread.
How do I set my laser printer on stun?
quote:Original post by Wildfire
Take a look at SuspendThread and ResumeThread.


Read his post more closely; he says "bring a thread to sleep (from inside the thread)." This would be slightly more complex than just SuspendThread; I think you would have to have a variable that can be modified by the thread, and one that can be read outside it. So you could change ThreadSleeping to true, then SuspendThread() outside the thread when the outside process notices that ThreadSleeping is true.

p.s. I''m assuming Win32 here, right?
quote:
Read his post more closely; he says "bring a thread to sleep (from inside the thread)." This would be slightly more complex than just SuspendThread; I think you would have to have a variable that can be modified by the thread, and one that can be read outside it. So you could change ThreadSleeping to true, then SuspendThread() outside the thread when the outside process notices that ThreadSleeping is true.


Why should he need someone outside the thread to call SuspendThread?

Here''s what the pause member function of my thread class looks like:

void Thread::pause(void){	if (_handle && _running)	{		_paused=true;		SuspendThread(_handle);	}}


It is perfectly possible to call ''pause'' from inside the ''run''-member-function (which is the actual thread). I think this is exactly what he is asking for. Of course I forgot to mention the variables like ''_paused'' etc. that can be checked from outside the thread. I thought it was kinda obvious that you''d need something like this.
How do I set my laser printer on stun?
Under Win32 you would use an Event, under posix you would simulate an event using a Condition Variable.

I guess you could fanagle it using SuspendThread and ResumeThread, but those are for debugging purposes. With an event the thread will be awoken as soon as the event is signaled.

[edited by - Magmai Kai Holmlor on November 17, 2003 10:53:25 PM]
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
depends how u want to suspend and resume. if u want the thread to control itself, u don''t want to be using SuspendThread. if a thread called SuspendThread on itself and no other threads are ''aware'' of it''s condition, how would it wake up? threads should almost always put themselves to sleep, seeing as they can be in critical sections when another thread suspends them.

like the other poster said, u want to use events. they are really simple in the win32 api. create them using CreateEvent and call SetEvent() to trigger. any thread waiting on the event will wake up when it is triggered. the wait function is WaitForSingleObject, although there are several other wait functions that include waiting on events, mutexes, windows messages, etc., most of them all in the same API call.

if u want to go unix, then your only *safe* bet afaik is systemV semaphores, although u can probably get away with posix mutexes. i don''t know of any cross-platform (i.e. portable) way of doing it. if someone does, please enlighten me.
Ok, what exactly is the problem with SuspendThread? Why are they for debug purpose only? What''s the problem about them.
Well, thanks for all your answers, I guess I should have a look at semaphores now.

This topic is closed to new replies.

Advertisement