[futurebasic] Re: Yielding Processor

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : December 1997 : Group Archive : Group : All Groups

From: Rick Brown <rbrown@...>
Date: Fri, 05 Dec 1997 21:42:41 -0600
> While still learning about  the FBII environment, I'm developing a tool
> that's pretty computationally intensive.  I'd like to yield some of the
> processor back to other applications, so they can do their thing for a while
> before I continue. Is there an easy way to do this? Does the DELAY command
> yield the processor to other threads?

DELAY does not, but HANDLEEVENTS does.  In addition to checking for
recent events, HANDLEEVENTS gives over control to the Process Manager,
which (if there aren't any events pending for you) may then decide to
hand control over to some other process (which presumably has been
patiently waiting at some instruction equivalent to HANDLEVENTS within
its own code).  When that other process then comes to its next
HANDLEEVENTS-like instruction (and no events are pending for that
process), then the Process Manager may return control back to _your_
process.  As long as all processes cooperate by periodically calling
HANDLEVENTS (or equivalent), then everybody gets a slice of time. 
That's what's meant by "cooperative multitasking."

To make it work, each process has to call HANDLEEVENTS _frequently_. 
That's because at any given moment, only one active process is actually
executing instructions--the rest are all "frozen" at their own
HANDLEEVENTS instructions.  And they _remain_ frozen until the
currently-executing process calls HANDLEEVENTS again (at which point
_it_ gets frozen while somebody else gets a chance).  To maintain the
illusion that all processes are executing "simultaneously," they each
have to call HANDLEEVENTS as frequently as they can manage.

Some day Apple is supposed to release an OS that does "preemptive
multitasking."  When that happens, you won't need to call HANDLEEVENTS
to yield the CPU, because the OS will use periodic hardware interrupts
to dole out the time to the various processes.  (You would _still_ have
to call HANDLEEVENTS to actually get events, however).

- Rick