Y
ysantoso-rubytalk
Hi,
I was wondering why a Continuation is not callable across threads.
From the definition at wikipedia:
"a continuation saves the execution state of a program at a given
point so that it's possible to resume execution from that state at a
later point in time."
If one takes a thread for what it actually is, which is a
stream-of-execution, then I fail to see a reason, beside the one described
below, why one thread cannot call a Continuation object generated from
another thread.
Why can't the thread use the execution state information saved within
a continuation to recreate the execution state, thus resuming the
suspended execution.
One argument against this capability is that the following code will
may not be correct:
$ = nil
t =Thread.new {
until $todo != nil; end
$todo.call
$todo = nil
}
def foo
Thread.current[:counter] = 0
callcc{|cc|
$todo = cc
return
}
puts "Counter is 0: #{Thread.current[:counter]}" # --> but counter may not be 0
# code depending on thread-specific storage does not operate
# correctly if resumed from another thread.
end
foo
t.join
However, I feel that this is more of a discipline issue on the part of
the programmer. If your code depends on thread-specific storage, don't
resume from another thread.
So, what is the reason for the restriction? Is there some conceptual
issue with continuations being callable from across threads, or is
this just implementation-specific issue?
Thanks,
YS.
I was wondering why a Continuation is not callable across threads.
From the definition at wikipedia:
"a continuation saves the execution state of a program at a given
point so that it's possible to resume execution from that state at a
later point in time."
If one takes a thread for what it actually is, which is a
stream-of-execution, then I fail to see a reason, beside the one described
below, why one thread cannot call a Continuation object generated from
another thread.
Why can't the thread use the execution state information saved within
a continuation to recreate the execution state, thus resuming the
suspended execution.
One argument against this capability is that the following code will
may not be correct:
$ = nil
t =Thread.new {
until $todo != nil; end
$todo.call
$todo = nil
}
def foo
Thread.current[:counter] = 0
callcc{|cc|
$todo = cc
return
}
puts "Counter is 0: #{Thread.current[:counter]}" # --> but counter may not be 0
# code depending on thread-specific storage does not operate
# correctly if resumed from another thread.
end
foo
t.join
However, I feel that this is more of a discipline issue on the part of
the programmer. If your code depends on thread-specific storage, don't
resume from another thread.
So, what is the reason for the restriction? Is there some conceptual
issue with continuations being callable from across threads, or is
this just implementation-specific issue?
Thanks,
YS.