如何取回多线程 tcl 的完成运行?
How to get back the finished runs of multithreading tcl?
我运行同时使用这 2 个计数器:
package require Thread
set TID1 [thread::create -joinable {
proc DATE {} {clock format [clock scan [exec date]] -format {%m%b%y-%H:%M:%S}}
puts "start T1-: [DATE]"
for {set i 0} {$i < 3} {incr i} {
exec sleep 1
puts "run T1 $i"
}
puts "end T1---: [DATE]"
}]
set TID2 [thread::create -joinable {
proc DATE {} {clock format [clock scan [exec date]] -format {%m%b%y-%H:%M:%S}}
puts "start T2-: [DATE]"
for {set j 0} {$j < 5} {incr j} {
exec sleep 1
puts "run T2 $j"
}
puts "end T2---: [DATE]"
}]
结果是:
% tid0x2aeab8e06700
% tid0x2aeab8c05700
% % % % start T2-: 09Sep21-16:03:27
start T1-: 09Sep21-16:03:27
run T2 0
run T1 0
run T2 1
run T1 1
run T2 2
run T1 2
end T1---: 09Sep21-16:03:30
run T2 3
run T2 4
end T2---: 09Sep21-16:03:32
如何捕获第一个完成的运行(即T2)并显示它?,
使用以下代码不起作用:
foreach TID {$TID1 $TID2} {
set FID [thread::join $TID]
puts "treads finished => $FID"
}
记住 Rule 8 部分说
Variable substitution is not performed on words enclosed in braces.
在创建 foreach
迭代的列表时,您需要 将 进行变量替换,例如
foreach TID [list $TID1 $TID2] {
set FID [thread::join $TID]
puts "thread finished => $FID"
}
我运行同时使用这 2 个计数器:
package require Thread
set TID1 [thread::create -joinable {
proc DATE {} {clock format [clock scan [exec date]] -format {%m%b%y-%H:%M:%S}}
puts "start T1-: [DATE]"
for {set i 0} {$i < 3} {incr i} {
exec sleep 1
puts "run T1 $i"
}
puts "end T1---: [DATE]"
}]
set TID2 [thread::create -joinable {
proc DATE {} {clock format [clock scan [exec date]] -format {%m%b%y-%H:%M:%S}}
puts "start T2-: [DATE]"
for {set j 0} {$j < 5} {incr j} {
exec sleep 1
puts "run T2 $j"
}
puts "end T2---: [DATE]"
}]
结果是:
% tid0x2aeab8e06700
% tid0x2aeab8c05700
% % % % start T2-: 09Sep21-16:03:27
start T1-: 09Sep21-16:03:27
run T2 0
run T1 0
run T2 1
run T1 1
run T2 2
run T1 2
end T1---: 09Sep21-16:03:30
run T2 3
run T2 4
end T2---: 09Sep21-16:03:32
如何捕获第一个完成的运行(即T2)并显示它?, 使用以下代码不起作用:
foreach TID {$TID1 $TID2} {
set FID [thread::join $TID]
puts "treads finished => $FID"
}
记住 Rule 8 部分说
Variable substitution is not performed on words enclosed in braces.
在创建 foreach
迭代的列表时,您需要 将 进行变量替换,例如
foreach TID [list $TID1 $TID2] {
set FID [thread::join $TID]
puts "thread finished => $FID"
}