我有这样的代码:
package require Thread
proc p1 {} {
set tid [thread::create {
proc executeCommand {command} {
return $command
}
thread::wait
}]
set result ""
::thread::send -async $tid [list executeCommand {"Hello thread world"}] result
#***Do some additional stuff***
vwait result
::thread::release $tid
puts $result
return $result
}
p1在找到保存这段代码的.tcl文件之后,我期望子线程在调用vwait之后返回"Hello线程世界“,并打印出'result‘变量,但这两种情况都没有发生。“结果”变量似乎仍然是空白的。
奇怪的是,当我将代码从过程(proc)块中取出并源源化.tcl文件时,它工作得很完美,但是根据我的系统设置方式,我需要使用过程。
不知道我做错了什么。
发布于 2017-03-25 10:18:17
“问题”是,接收变量(与vwait一样)位于全局命名空间,而不是当前范围内的变量;在 in the callback上使用标志TCL_GLOBAL_ONLY (而Tcl的底层变量实现非常复杂,因此如果可能的话,我们确实希望坚持使用API ):
/*
* Set the result variable
*/
if (Tcl_SetVar2Ex(interp, var, NULL, valObj,
TCL_GLOBAL_ONLY | TCL_LEAVE_ERR_MSG) == NULL) {
rc = TCL_ERROR;
goto cleanup;
}一般来说,这是有意义的,因为您可以从启动后台线程到接收结果之间的过程中返回,而Tcl确实试图避免早期绑定。
结果怎么样了?它位于全局result变量中( ::只是表示“我真的想使用名为this的全局变量”):
% puts $::result
"Hello thread world"最简单的解决方法是使用一个变量来完成特定调用所特有的接收。这听起来比实际更复杂,因为我们已经在线程ID中获得了一个唯一的令牌:
proc p1 {} {
set tid [thread::create {
proc executeCommand {command} {
return $command
}
thread::wait
}]
### Make the name of the global variable (an array element) ###
set var ::p1results($tid)
### This is a simple transformation of what you were already doing ###
set $var ""
::thread::send -async $tid [list executeCommand {"Hello thread world"}] $var
#***Do some additional stuff***
vwait $var
### Transfer the global variable into a local and remove the global ###
set result [set $var]
unset $var
### Back to your code now ###
::thread::release $tid
puts $result
return $result
}当我试用它的时候,这看起来像预期的那样起作用了。
https://stackoverflow.com/questions/43009030
复制相似问题