[plt-scheme] Parameters and Eventspaces Question
At Tue, 28 Oct 2008 12:41:08 -0600, "Doug Williams" wrote:
> I assume the problem is with my mental model of parameters and eventspaces.
> I had assumed that each eventspace would have, in addition to its event
> queues, etc, its own execution thread, and copies of any parameters.
That's correct.
> Since
> it allows me to parameterize or set the current-frame in a new eventspace, I
> assume the eventspace 'knows about' my current-frame parameter. What I
> don't understand is why a call to update-text-field by a frame that I think
> is running under 'new' eventspace doesn't get a value for the current-frame
> parameter.
In
(parameterize ([current-eventspace E])
B)
the `parameterize' sets the `current-eventspace' parameter to `E' while
evaluating `B', it still evaluates `B' in the current thread --- not
the handler thread of `E'. So if you mutate a parameter within `B',
then it still mutates the parameter for the current thread, not for the
thread of `E'.
Use `queue-callback' to jump to another eventspace's handler thread.
The `queue-callback' function uses the `current-eventspace' parameter
(of the current thread) to determine where to queue the callback, and
the callback is invoked in that eventspace's handler thread.
(define (call-in-other-eventspace e thunk)
(let ([ch (make-channel)])
(parameterize ([current-eventspace e])
(queue-callback (lambda ()
(channel-put ch (thunk)))))
(channel-get ch)))
(call-in-other-eventspace
(make-eventspace)
(lambda ()
(parameterize ((current-frame (instantiate my-frame%
("Frame 2"))))
(send (current-frame) show #t)
(update-text-field))))
(call-in-other-eventspace
(make-eventspace)
(lambda ()
(let ((frame-3 (instantiate my-frame%
("Frame 3"))))
(current-frame frame-3)
(send (current-frame) show #t)
(update-text-field))))