Re: Multitasking: active waiting!?
- Posted by Jason Gade <jaygade at yahoo.com> Apr 19, 2006
- 475 views
Ryan W. Johnson wrote: > > Robert Craig wrote: > > > The way it works at the moment, the Euphoria task scheduler > > calls sleep() only when there is a gap of at least 1 second > > until the next task is due to resume. That's because sleep() only > > has a resolution of 1 second (that's the worst case > > across all platforms). For smaller gaps it just spins its > > wheels in a loop. > > That's what i thought was happening. > > > It might be wasteful to sleep for a tiny fraction of a second, > > giving up control, and hoping the O/S will give it back in time. > > Also, other than DOS, the O/S should be able to grab control > > pre-emptively whenever it wants to run another process, > > regardless of whether you are sleeping or not. > > The problem I have with that is the program is hogging all the available CPU > instead of letting the CPU idle. It's true that the OS can still give other > processes a chance to run, but the point is it's not letting the CPU idle like > it should be when it's not "doing" anything. > > I'm wondering how well it would work if the scheduler tried to sleep for short > intervals (perhaps 0.01 s). > > Here's an idea: if you made the sleep() procedure more precise, then the > programmer could easily create an > idle task. > > }}} <eucode> > global procedure idle(atom idletime) > while 1 do > sleep(idletime) > task_yield() > end while > end procedure > > task_idle = task_create(routine_id("idle"), {0.01}) --create task to idle for > 0.01 seconds. > task_schedule(task_idle, {1}) --time-shared task > </eucode> {{{ > > "Real-time tasks have a higher priority. Time-shared tasks are run when no > real-time > task is ready to execute." > If i understand this correctly, the above code would call the task_idle() only > when real-time tasks are not scheduled to run, right? > > If you think it's a good idea, perhaps idle() could even be a built-in > routine. > What do you think about all this, Rob? > > ~Ryan W. Johnson > > Fluid Application Environment > <a href="http://www.fluidae.com/">http://www.fluidae.com/</a> > > [cool quote here, if i ever think of one...] I think the tasking would work best for programs that always needed to be doing something. If a program needed to do nothing for awhile, then call task_clock_stop() for as long as it needs to wait. Then restart tasking when it needs to. -- "Any programming problem can be solved by adding a level of indirection." --anonymous "Any performance problem can be solved by removing a level of indirection." --M. Haertel "Premature optimization is the root of all evil in programming." --C.A.R. Hoare j.