Re: A sleeping task

new topic     » goto parent     » topic index » view thread      » older message » newer message

Juergen Luethje wrote:
> There's something else that I don't understand. Hopefully at least the
> question is not too stupid. smile
> 
> "Who" has a benefit when "someone else" sleeps?
> As far as I understand, when my program uses sleep(), other programs
> that run on the machine at the same time have a benefit. But --
> depending on the purpose of the program and the situation -- it might be
> a disadvantage that it's not possible to let only a single task sleep().
> 
> Using your task_sleep() from above, it's possible to let individual
> tasks sleep. Will this be of benefit "only" for the other tasks in my
> program, or also for other programs on the machine?

It might only benefit the other tasks in your program,
but if the scheduler can't find any task ready to run, it will
calculate the time until the next task will be ready, and then
it will call sleep(), which will benefit other programs.

> I'm thinking of a situation like this:
> Say we have a database program with a GUI. I think a good architecture
> for the program would be that the primary thread creates all the
> program's windows, contains all the window procedures for these windows,
> and processes all messages to the windows. Secondary threads carry out
> background jobs such as reading files from the disk.
> So when a secondary thread reads a huge database, we do not only want to
> prevent the GUI of our program from "freezing", but also other programs
> on the machine. Maybe we could (on Windows) do something like the
> following (probably not correct, but you'll get the idea):
> 
> <pseudo code>
> procedure task_sleep (atom n)
>    task_schedule(task_self(), {n, n})
>    task_yield()
>    if (PeekMessage(msg, NULL, 0, 0, PM_REMOVE)) then
>       TranslateMessage(msg)
>       DispatchMessage (msg)
>    end if
> end procedure
> </pseudo code>

Yes, in Windows you will want to check for messages at regular intervals
to keep the user interface alive. You might have a task just for
this purpose or you might have many tasks that check.

If you have a huge database to read, and you want to be friendly
to other programs, you might have a task that reads a few records 
and then yields for a small amount of time, or you might 
have a "time-shared" task that gets control from time to time, 
but releases control after doing a bit of work. If your program can't
use the time, the scheduler will sleep. If your program can use the time
but you want to be really friendly, you could have a task that 
explicitly calls sleep() every now and then.

Currently the Euphoria sleep() function has a 
resolution of 1 second. I'd like to improve that.
 
My experience has been that Windows is nowhere near as good as Linux
at running multiple programs at the same time. It seems like if you try
to run 3 things (say) at the same time, instead of each program
running at 1/3 the speed, they run at 1/10 the speed, and switching
between them is very slow.

> BTW: In the current documentation of task_schedule(), you always use
> integers. Is it also possible to use e.g.:
>      task_schedule(i, {0.1, 0.2}) ?

Yes, you can use fractions.
I'll document this better.

The resolution of time() in Windows is .01 seconds,
and that's fine for most purposes,
but the scheduler has a rough mechanism that lets you
specify times less than 0.01 seconds. In this case it will 
run your task multiple times per 0.01 seconds to try to 
simulate what you want. For example in Language War, 
I try to schedule the drawing of a "dot" for a phasor 
every 0.001 seconds. The scheduler can't measure times 
with that resolution, so it runs the phasor task up to 10 
times in a row before stopping and waiting for the time
to click ahead by 0.01. This was sort of experimental, but I
guess I should document it now.

Regards,
   Rob Craig
   Rapid Deployment Software
   http://www.RapidEuphoria.com

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu