|
From: Lars H. <Lar...@re...> - 2008-08-28 23:04:19
|
Alexandre Ferrieux skrev:
> Lars, I'd really appreciate your answer to this.
> Regards,
> -Alex
Oh, I've been busy with improving the proof-of-concept implementation
(just used it to implement an oracle for a nondeterministic
graph-colouring algorithm, but it's too late to put that on the Wiki
tonight).
> On 8/27/08, Alexandre Ferrieux <ale...@gm...> wrote:
>> Hello,
>>
>> On Sun, Aug 24, 2008 at 10:02 PM, Lars Hellström
>> <Lar...@re...> wrote:
>>> [suspend/resume, serialization of state]
>> Lars, aside from the discussion on the How, I'd like a bit more on the Why.
>> Specifically, I fail to map your "basic premise" example onto a
>> realistic engineering need.
Essentially, that is "keeping a GUI alive during a long-running
computation". Suppose for example that you want to show some kind of
animation. A very basic approach for doing this is
foreach frame $animation {
show_frame $frame
after $frame_period
}
but this doesn't work in Tcl/Tk, since we never run the event loop. It
can however be made to work if the [after] is replaced by an
[after]-[vwait] combination:
set ::tick 0
foreach frame $animation {
show_frame $frame
after $frame_period {incr ::tick}
vwait ::tick
}
Not only is the display updated, but furthermore the GUI is alive.
There is however a serious limitation with this approach -- it only
lets you run one such animation at a time, because [vwait]s nest; if
you start a second such command when in the [vwait] of the first, then
the first animation freezes until the second has run to end.
With my design, and provided a suitable TCL_SUSPEND handler is
installed in [bgerror], the above could have been coded as
foreach frame $animation {
show_frame $frame
suspend after $frame_period
}
without blocking, and keeping the GUI alive. This would work as
follows: the TCL_SUSPEND handler looks at what was the first argument
of [suspend], and interprets it as a sort of subcommand. In this case
it is "after", so the continuation should simply be resumed after a
specified length of time. Practically this would amount to the handler
doing
after [lindex $continuation 1] [list resume $continuation]
([lindex $continuation 1] being the $frame_period from above).
That much can be done with the NRE [coroutine] too -- however it turned
out that the basic [suspend] and [resume] primitives I came up with to
solve the above problem could also do a lot more than this.
And the basic problem of running multiple animations simultaneously can
of course be handled with only Tcl/Tk 8.5 features, as is the case with
anything that [coroutine] lets you do AFAICT, but this may require you
to restructure your code in a rather unintelligible way.
>> Also, from time to time people hint at the fact that passing a
>> continuation or coroutine across the thread/interp boundary would be
>> Overly Cool, but again I'd like a few examples of what real problems
>> this would solve.
I have no idea why that should be cool -- Miguel had however mentioned
it as something the NRE [coroutine]s cannot do, so I thought it worth
pointing out that you get it for free with [suspend]/[resume]. Maybe he
can elaborate on the usefulness of this however.
Lars Hellström
|