为什么 TCL 线程不打印输出
Why TCL threads are not printing the output
我正在使用 TCL 线程。我正在尝试编写一个简单的程序,它将打开 3 个线程,每个线程中只有一个简单的打印语句。
下面是我的代码
package require Thread
puts "*** I'm thread [thread::id]"
# Create 3 threads
for {set thread 1} {$thread <= 3} {incr thread} {
set id [thread::create {
# Print a hello message 3 times, waiting
# a random amount of time between messages
for {set i 1} {$i <= 3} {incr i} {
after [expr { int(500*rand()) }]
puts "Thread [thread::id] says hello"
}
}] ;# thread::create
puts "*** Started thread $id"
} ;# for
puts "*** Existing threads: [thread::names]"
# Wait until all other threads are finished
while {[llength [thread::names]] > 1} {
after 500
}
puts "*** That's all, folks!"
下面是输出
*** I'm thread tid00004028
*** Started thread tid0000A5E8
*** Started thread tid00009F28
*** Started thread tid00009D54
*** Existing threads: tid00009D54 tid00009F28 tid0000A5E8 tid00004028
*** That's all, folks!
从您使用 Tcl 8.4 的评论来看,您将获得的建议很简单:升级 到受支持的版本的 Tcl。我已经尝试了您在 OSX 上使用 tclsh8.5 和 tclsh8.6 发布的确切代码,并且都产生了人们可能期望的输出。这是一个 运行 和 8.5:
*** I'm thread tid0x7fff758f3180
*** Started thread tid0x104326000
*** Started thread tid0x1043ac000
*** Started thread tid0x1044b2000
*** Existing threads: tid0x1044b2000 tid0x1043ac000 tid0x104326000 tid0x7fff758f3180
Thread tid0x1043ac000 says hello
Thread tid0x104326000 says hello
Thread tid0x104326000 says hello
Thread tid0x1044b2000 says hello
Thread tid0x1043ac000 says hello
Thread tid0x1044b2000 says hello
Thread tid0x104326000 says hello
Thread tid0x1044b2000 says hello
Thread tid0x1043ac000 says hello
*** That's all, folks!
其他平台上的线程 ID 可能完全不同(它们的格式不是规范的一部分),但这应该是您得到的那种东西。当然,顺序中的模数变化。
即使安全修复也不再支持 Tcl 8.4;它在 8.4.20 发布后就停产了。
我正在使用 TCL 线程。我正在尝试编写一个简单的程序,它将打开 3 个线程,每个线程中只有一个简单的打印语句。
下面是我的代码
package require Thread
puts "*** I'm thread [thread::id]"
# Create 3 threads
for {set thread 1} {$thread <= 3} {incr thread} {
set id [thread::create {
# Print a hello message 3 times, waiting
# a random amount of time between messages
for {set i 1} {$i <= 3} {incr i} {
after [expr { int(500*rand()) }]
puts "Thread [thread::id] says hello"
}
}] ;# thread::create
puts "*** Started thread $id"
} ;# for
puts "*** Existing threads: [thread::names]"
# Wait until all other threads are finished
while {[llength [thread::names]] > 1} {
after 500
}
puts "*** That's all, folks!"
下面是输出
*** I'm thread tid00004028
*** Started thread tid0000A5E8
*** Started thread tid00009F28
*** Started thread tid00009D54
*** Existing threads: tid00009D54 tid00009F28 tid0000A5E8 tid00004028
*** That's all, folks!
从您使用 Tcl 8.4 的评论来看,您将获得的建议很简单:升级 到受支持的版本的 Tcl。我已经尝试了您在 OSX 上使用 tclsh8.5 和 tclsh8.6 发布的确切代码,并且都产生了人们可能期望的输出。这是一个 运行 和 8.5:
*** I'm thread tid0x7fff758f3180 *** Started thread tid0x104326000 *** Started thread tid0x1043ac000 *** Started thread tid0x1044b2000 *** Existing threads: tid0x1044b2000 tid0x1043ac000 tid0x104326000 tid0x7fff758f3180 Thread tid0x1043ac000 says hello Thread tid0x104326000 says hello Thread tid0x104326000 says hello Thread tid0x1044b2000 says hello Thread tid0x1043ac000 says hello Thread tid0x1044b2000 says hello Thread tid0x104326000 says hello Thread tid0x1044b2000 says hello Thread tid0x1043ac000 says hello *** That's all, folks!
其他平台上的线程 ID 可能完全不同(它们的格式不是规范的一部分),但这应该是您得到的那种东西。当然,顺序中的模数变化。
即使安全修复也不再支持 Tcl 8.4;它在 8.4.20 发布后就停产了。