Archived

This topic is now archived and is closed to further replies.

LonelyStar

Bringing Threads to sleep and waking them

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites