Re: Multitasking: active waiting!?
- Posted by Ryan W. Johnson <ryanj at fluidae.com> Apr 19, 2006
- 499 views
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.
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
"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 http://www.fluidae.com/ [cool quote here, if i ever think of one...]