首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TCL线程::发送未设置的upvar变量

TCL线程::发送未设置的upvar变量
EN

Stack Overflow用户
提问于 2017-03-24 20:57:51
回答 1查看 314关注 0票数 1

我有这样的代码:

代码语言:javascript
复制
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文件时,它工作得很完美,但是根据我的系统设置方式,我需要使用过程。

不知道我做错了什么。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-25 10:18:17

“问题”是,接收变量(与vwait一样)位于全局命名空间,而不是当前范围内的变量;在 in the callback上使用标志TCL_GLOBAL_ONLY (而Tcl的底层变量实现非常复杂,因此如果可能的话,我们确实希望坚持使用API ):

代码语言:javascript
复制
/*
 * 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的全局变量”):

代码语言:javascript
复制
% puts $::result
"Hello thread world"

最简单的解决方法是使用一个变量来完成特定调用所特有的接收。这听起来比实际更复杂,因为我们已经在线程ID中获得了一个唯一的令牌:

代码语言:javascript
复制
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
}

当我试用它的时候,这看起来像预期的那样起作用了。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43009030

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档