use the -async
flag for thread::send
. By default, ::thread::send
blocks until the script has been executed (which defeats most of the time the use of threads).
If you use the -async
flag, you can also use the optional variable argument to thread::send
and vwait
for that, e.g.
set tid [thread::create {
proc fib n {
if {$n == 0 || $n == 1} {return 1}
return [expr {[fib [expr {$n - 1}]] + [fib [expr {$n - 2}]]}]
}
thread::wait
}]
::thread::send -async $tid [list fib $num] result
vwait result
::thread::release $tid
# ... do something with result
This should prevent the GUI from freezing. Note that this implementation of the fibonacci is not the best and is a placeholder for "some expensive calculation".