1. A sleeping task

Hi all,

in the new multitasing system, when sleep() is called in one task, not
only this task "gives some time slices to the operating system", but the
whole Euphoria program goes to bed.

Asking mainly out of curiosity: Is it possible and/or desirable that
sleep() would only affect the task that calls it?

Regards,
   Juergen

-- 
Have you read a good program lately?

new topic     » topic index » view message » categorize

2. Re: A sleeping task

Juergen Luethje wrote:
> in the new multitasing system, when sleep() is called in one task, not
> only this task "gives some time slices to the operating system", but the
> whole Euphoria program goes to bed.
> 
> Asking mainly out of curiosity: Is it possible and/or desirable that
> sleep() would only affect the task that calls it?

That's a good question.
I guess in some cases you might want the whole
program to sleep, and in other cases you might want only
the current task to sleep. 

To make just the current task sleep, you could code
something like:
task_schedule(task_self(), {3, 3})
     task_yield()

This would make the current task sleep for 3 seconds,
while allowing other tasks to run. You could even wrap
this into a useful routine (task_sleep(n)?)

Since, even when multitasking is used, it might be useful 
to make the whole program sleep, I'll leave sleep() 
as it is, but I'll document this issue.

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

new topic     » goto parent     » topic index » view message » categorize

3. Re: A sleeping task

Robert Craig wrote:

> Juergen Luethje wrote:
>
>> in the new multitasing system, when sleep() is called in one task, not
>> only this task "gives some time slices to the operating system", but the
>> whole Euphoria program goes to bed.
>>
>> Asking mainly out of curiosity: Is it possible and/or desirable that
>> sleep() would only affect the task that calls it?
>
> That's a good question.
> I guess in some cases you might want the whole
> program to sleep, and in other cases you might want only
> the current task to sleep.
>
> To make just the current task sleep, you could code
> something like:
> }}}
<eucode>
>      task_schedule(task_self(), {3, 3})
>      task_yield()
> </eucode>
{{{

> This would make the current task sleep for 3 seconds,
> while allowing other tasks to run. You could even wrap
> this into a useful routine (task_sleep(n)?)

Ahhh, cool. smile  Thanks!
At least for a layman, this is not obvious. So I think it would be
helpful, either to officially provide a function task_sleep(n), or
to include this exanmple in the documentation.

> Since, even when multitasking is used, it might be useful
> to make the whole program sleep, I'll leave sleep()
> as it is, but I'll document this issue.


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?

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>


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}) ?

Thanks for your interesting information.

Regards,
   Juergen

-- 
Have you read a good program lately?

new topic     » goto parent     » topic index » view message » categorize

4. Re: A sleeping task

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 message » categorize

5. Re: A sleeping task

Robert Craig wrote:

[a lot]

Thanks for your comprehensive explanations!!
I learn a lot here on this list. smile

Regards,
   Juergen

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu