在线程之间传递用 C 语言创建的 tcl 命令



是否可以在用 C 创建的 TCL 线程(使用 TCL 命令 -thread::create创建)命令之间传递(即使用Tcl_CreateObjCommand)以及如何传递?

谢谢。

所有 Tcl 命令总是耦合到特定的解释器,解释器作为其第一个参数传递给Tcl_CreateObjCommand,Tcl 解释器严格绑定到线程(因为 Tcl 实现在内部使用相当多的线程特定变量以减少全局锁的数量)。相反,实现通过消息在线程之间进行协调;最常见的消息类型是"这是为我运行的 Tcl 脚本"和"这是运行该脚本的结果",尽管还有其他消息。

所以不,Tcl 命令不能在线程之间共享。如果你为它们编写了正确的代码(通常通过避免全局或添加适当的锁),你可以在多个线程的多个解释器中使用相同的命令实现,但它们在技术上不是相同的命令,而是乍一看是一样的。例如,如果您在一个线程中对命令进行跟踪,则只会在该解释器中调用其回调,而不会从具有相同实现和相同名称的命令的任何其他解释器调用。


您可以在其他线程中生成委托命令,要求主线程运行该命令并将结果发回给您。

package require Thread
# This procedure makes delegates; this is a little messy...
proc threadDelegateCommand {thread_id command_name} {
# Relies on thread IDs always being “nice” words, which they are
thread::send $thread_id [list proc $command_name args "
thread::send [thread::id] [list [list $command_name] {*}$args]
"]
}
# A very silly example; use your code here instead
proc theExampleCommand {args} {
puts "This is in [thread::id] and has [llength $args] arguments: [join $args ,]"
return [tcl::mathop::+ {*}$args]
}
# Make the thread
set tid [thread::create]
puts "This is [thread::id] and $tid has just been created"
# Make the delegate for our example
threadDelegateCommand $tid theExampleCommand
# Show normal execution in the other thread
puts [thread::send $tid {format "This is %s" [thread::id]}]
# Show that our delegate can call back. IMPORTANT! Note that we're using an asynchronous
# send here to avoid a deadlock due to the callbacks involved.
thread::send -async $tid {
after 5000
theExampleCommand 5 4 3 2 1
} foo
vwait foo

最新更新