我正在使用TCL线程。我正在尝试编写一个简单的程序,它将打开3个线程,并在每个线程中只打一个简单的print语句。
以下是我的代码
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!
答案 0 :(得分:2)
由于您使用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发布后,这是生命终结。