ted brown <
tedbr...@gmail.com> wrote:
> On 8/30/2021 8:23 PM, Rich wrote:
>
> Hmmm, interesting. I think I see, just a few follow-up questions.
>
>>
>> parent thread code (assume Tk is running):
>>
>> set t1 [thread::create {
>> proc do-something {a b c} {
>> lappend result [do-something1 $a] ;# assume do-something
>> lappend result [do-something2 $b]
>> lappend result [do-something3 $c]
>> # assume this thread was 'informed' somehow of the parent thread ID
>> # (tsv is useful for this)
>> thread::send -async $parent [list the-answer-is $result]
>> }
>> }
>
> So, this seems similar to wish/tclsh running a script where if wish,
> reaching the end of the script file it enters the event loop, while if
> started by tclsh, it exits instead - or one uses an explicit vwait.
Yes.
> So, analogously reaching the end of the thread script also either enters
> the event loop (if started with wish) or exits (if started by tclsh).
This has been my experience so far.
> Is that the right way to think about it?
It seems reasonable. Others may wish to add corrections.
> And so the thread::wait does what the vwait does in the regular script
> when you startup with tclsh.
>
> Now with regular events, if the code triggered by an event does a vwait
> itself, then the next queued event will trigger, which in our case here,
> with just the 1 proc, would re-enter do-something interrupting the
> earlier call to do-something. Is that correct?
This is very likely, so you would need to use care (as always) when
having a proc in a thread perform an additional vwait.
> So to keep do-something from being interrupted, one must not do
> anything, like an update, or vwait. Or else, I guess to keep them
> sequential, one would have to use some thread locking primitives.
Either would be possible, and the threads package does provide the
usual set of sync. primitives. But with the exception of needing to
handle code that does a 'vwait' you don't otherwise have to worry about
these items.
>> proc the-answer-is {result} {
>> display-the-answer $result ;# assume 'display-the-answer' does
>> # whatever necessary to show/print/output/etc. the answer
>> }
>
>
> So, this proc is similar to one that might be called via the script arg
> in a bind call except the "bounded" event is the thread::send back to
> the parent.
In a way, yes. The flow becomes event driven. Button press event
results in "message send" event to the other thread. Later, when the
other thread finishes, it creates another "message send" event back to
the parent (or to some other 'next' thread).
>> button .b -text "Do something" \
>> -command [list thread::send -async $t1 do-something $z $y $x]
>> # assume z y x are -textvars connected to other widgets
>>
>
> Is z, y, and x, being connected to widgets just as an example, or is
> there something special about those 3 variables?
Just example names to fill in the psudeocode. I.e., think of them as
being attached to entry widgets via the -textvariable option.
> Also, I thought the script had to be in one arg, should that be
>
> -command [list thread::send -async $t1 [list do-something $z $y $x]]
Yes, you are correct. I did omit another "listification" there. Well,
I did call it "psudeocode". :)
>> This is sort of a RPC/message hybrid concept. The individual async
>> thread send's are passing messages around. The messages execute
>> "remote procedures".
>>
>
> Thanks so much, I think I now have a way to think about it that is
> not too different from what I'm used to - assuming my above thinking
> is correct :)
You appear (to the extent I can tell via Usenet) to be on a reasonable
path. And, as always, one of the best ways to learn is to give it a
try.