2016-04-05 97 views
1

我正在使用TCL線程。我正在嘗試編寫一個簡單的程序,它將在每個線程中打開3個線程和一個簡單的打印語句。爲什麼TCL線程不打印輸出

下面是我的代碼

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! 
+2

適用於我,當我嘗試確切的代碼。 –

+0

什麼是打印?你可以顯示輸出 – Nitesh

+0

我在窗口中使用Tcl 8.4 – Nitesh

回答

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發佈之後,它終結了。

+0

僅供參考,我的問題是,Thread包是否內置在tclsh8.6中?當我嘗試使用tclsh8.4訪問線程時,Thread包不在那裏。我的理解是否正確? –

+1

線程包在8.4中是外部的。它應該與8.6一起出現,除非有人做了一個真正的裸機構建,跳過了所有的貢獻包。 –