Re: A sleeping task

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

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 thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu