我正在使用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的运行:
***我是线程tid0x7fff758f3180***已启动线程tid0x104326000***已启动线程tid0x1043ac000***已启动线程tid0x1044b2000***现有线程:tid0x1044b2000 tid0x1043ac000 tid0x04326000 tid0x7fff758f3180线程tid0x1043ac000打招呼线程tid0x104326000打招呼线程tid0x104326000打招呼线程tid0x1044b2000打招呼线程tid0x1043ac000打招呼线程tid0x1044b2000打招呼线程tid0x104326000打招呼线程tid0x1044b2000打招呼线程tid0x1043ac000打招呼***仅此而已,伙计们!
线程ID在其他平台上可能有很大不同(它们的格式不属于规范的一部分),但这应该是你得到的东西。当然,排序中的模数变化。
Tcl 8.4甚至不再支持安全修复;它在8.4.20发布后就报废了。