2016-04-05 4 views
1

Ich verwende TCL-Threads. Ich versuche, ein einfaches Programm zu schreiben, das 3 Threads und nur eine einfache Druckanweisung in jedem Thread öffnet.Warum TCL-Threads die Ausgabe nicht drucken

Unten finden Sie meine Code

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!" 

Unterhalb der Ausgang

*** I'm thread tid00004028 
*** Started thread tid0000A5E8 
*** Started thread tid00009F28 
*** Started thread tid00009D54 
*** Existing threads: tid00009D54 tid00009F28 tid0000A5E8 tid00004028 
*** That's all, folks! 
+2

Funktioniert für mich, wenn ich den genauen Code versuche. –

+0

Was ist das Drucken? Können Sie die Ausgabe zeigen – Nitesh

+0

Ich verwende Tcl 8.4 in Windows – Nitesh

Antwort

2

wird aus den Kommentaren Da Sie Tcl verwenden 8.4, die Beratung, die Sie sich zu erhalten ist einfach: Upgrade zu einer unterstützten Version von Tcl. Ich habe den genauen Code ausprobiert, den Sie sowohl mit tclsh8.5 als auch mit tclsh8.6 auf OSX gepostet haben, und beide haben genau die Ausgabe produziert, die Sie erwarten könnten. Hier ist ein Lauf mit 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! 

Das Thread-IDs kann ganz unterschiedlich sein auf anderen Plattformen (ihr Format ist nicht Teil der Spezifikation), aber das sollte die Art der Sache sein, die Sie erhalten. Modulo-Variation in der Bestellung natürlich.

Tcl 8.4 wird nicht mehr für selbst Sicherheitsfixes unterstützt; Es war Ende des Lebens nach dem 8.4.20 wurde veröffentlicht.

+0

Nur zur Information, meine Frage ist, ist Thread-Paket in tclsh8.6 eingebaut? wo das Thread-Paket nicht vorhanden ist, wenn ich versuche, mit tclsh8.4 auf Thread zuzugreifen. Ist mein Verständnis richtig? –

+1

Das Thread-Paket war extern in 8.4. Es sollte mit 8.6 vorhanden sein, es sei denn, irgendjemand macht einen wirklich blanken Build, der alle beigesteuerten Pakete überspringt. –