Tcl线程:如何访问线程中的全局变量

萨兰

我有一个名为“ startMyProc {num}”的进程。我希望该过程由两个不同的线程调用,并等待两个线程完成。我尝试了给出的有效解决方案。我想访问startMyProc中的全局变量并调用另一个proc“ startMyAnotherProc {num}”。如何才能做到这一点?

package require Thread


global myVar

set myVar false

set id1 [thread::create -joinable {
    source sample.tcl
    thread::wait
    }]
set id2 [thread::create -joinable {
    source sample.tcl
    thread::wait
    }]

set num 1
thread::send -async $id1 [list startMyProc $num]
set num 2
thread::send -async $id2 [list startMyProc $num]

thread::join $id1
thread::join $id2

My sample.tcl looks like this,

proc startMyProc { num } {
    global myVar
    puts $myVar
    puts "Opening $num"
    after 2000
    puts "Opening $num"
    after 2000
    puts "Opening $num"
    after 2000
    startMyAnotherProc $myVar
    return
}

proc startMyAnotherProc { num } {
    puts "Opening Another Proc: $num"
    after 2000
    puts "Opening Another Proc: $num"
    after 2000
    return
}
多纳研究员

每个线程都有自己的完整解释器,与程序中的所有其他解释器隔离(thread软件包的命令功能除外)。在所有线程中获取过程的最简单,最直接的方法是将其放在脚本文件中,然后将source其作为线程启动脚本的一部分:

set t1 [thread::create -joinable {
    source myProcedures.tcl
    startMyProc $num
}]
set t2 [thread::create -joinable {
    source myProcedures.tcl
    startMyProc $num
}]

但是您会遇到另一个问题。变量不能共享。那意味着您不会克服$num您应该真正使脚本启动,然后thread::wait在最后完成。然后,您可以将thread::send其作为工作(并在构建脚本时获得正确的替换)。

set t1 [thread::create -joinable {
    source myProcedures.tcl
    thread::wait
}]
set t2 [thread::create -joinable {
    source myProcedures.tcl
    thread::wait
}]
thread::send -async $t1 [list startMyProc $num]
thread::send -async $t2 [list startMyProc $num]

但是,如果您真的在考虑将任务发送给工作线程,则应该查看线程池(tpool)支持;这是很多以扩大更容易。

# Make the thread pool
set pool [tpool::create -initcmd {
    source myProcedures.tcl
}]

# Sent the work into the pool as distinct jobs
set job1 [tpool::post $pool [list startMyProc $num]]
set job2 [tpool::post $pool [list startMyProc $num]]

# Wait for all the jobs in the pool to finish
set waitingfor [list $job1 $job2]
while {[llength $waitingfor] > 0} {
    tpool::wait $pool $waitingfor waitingfor
}

# Get results now with tpool::get

# Dispose of the pool
tpool::release $pool

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Python全局变量未在线程中更新

来自分类Dev

如何使用全局变量的值来确定在单独线程中运行的函数的控制流?

来自分类Dev

如何在r函数中访问全局变量

来自分类Dev

如何在Python中访问全局变量?

来自分类Dev

用线程增加全局变量

来自分类Dev

用Python中的线程更改全局变量

来自分类Dev

使用全局变量的WebSocket线程

来自分类Dev

如何动态访问Node中的全局变量?

来自分类Dev

如何访问模板中的全局变量?

来自分类Dev

从线程全局更新变量并从python中的main访问

来自分类Dev

python线程:事件与全局变量

来自分类Dev

使用线程打印全局变量

来自分类Dev

如何在Kivy文件中访问全局变量?

来自分类Dev

如何访问全局变量

来自分类Dev

线程访问全局变量并强制杀死旧线程

来自分类Dev

如何在php中的线程之间共享全局变量?

来自分类Dev

如何在子程序中访问全局变量

来自分类Dev

如何从Node中的required()文件访问全局变量?

来自分类Dev

如何在线程外更新全局变量?

来自分类Dev

可以从新进程中的线程访问全局变量吗?

来自分类Dev

如何访问在主线程中定义的变量?

来自分类Dev

如何在Python中访问全局变量?

来自分类Dev

Python线程全局变量问题

来自分类Dev

用线程增加全局变量

来自分类Dev

Sidekiq线程访问全局变量

来自分类Dev

在C线程编程中未正确获取全局变量

来自分类Dev

多个批处理文件访问/设置相同的共享/全局变量线程安全吗?

来自分类Dev

使用全局变量的线程问题

来自分类Dev

如何避免线程应用程序中的全局变量