【发布时间】:2017-07-08 16:30:55
【问题描述】:
我想以同步模式向线程发送消息并使用跟踪变量方法获取结果。问题是我没有得到线程的任何响应。当我以正常模式发送消息时(thread:: send thread_id {command} var),我也会得到保存在 var 中的结果。谁能指出我犯错的地方?下面我传递我的代码:
trace add variable res write {apply {{v1 v2 op} {
upvar 1 $v1 v
puts "updated variable to $v"}}}
set th [thread::create {
puts "new thread id : [thread::id]"
proc fun {n} {
return [expr {$n*2}]
}
thread::wait
}]
# thread::send $th [list fun $t] res
thread::send -async $th [list fun 43] res
【问题讨论】:
-
thread manual page 声明:“如果指定了 -async 标志,则该命令不会等待结果并返回空字符串。”你期待会发生什么?变量不跨线程共享,无法跨线程追踪。
-
感谢您的回答。我认为一段时间后 res 值会发生变化,我将能够异步注册此事件。还有其他方法可以在不使用 vwait 的情况下在主线程中注册线程的结尾吗?
标签: multithreading tcl