namespace eval threads { variable threads [list main [thread::id]] variable log [logger::init tclircc::threads] proc manage {tid name} { variable threads variable log dict set threads $name $tid ${log}::debug "managing thread $tid as \"$name\"" thread::send -head $tid { namespace eval t { variable ns proc update {} { variable ns thread::send -head [set ns(main)] threads::update } proc ns {name} { variable ns set ns($name) } } } ::threads::update } proc unmanage {name} { variable threads variable log dict unset threads $name ${log}::debug "unmanaging thread $tid (\"$name\")" ::threads::update } proc update {} { variable threads dict for {name tid} $threads { if ![thread::exists $tid] {dict unset $threads $name} } dict for {name tid} $threads { set payload {array unset t::ns; } dict for {n t} $threads { append payload {set t::ns(} $n {) } $t {; } } thread::send -head $tid $payload } ::update } proc debug {} { variable log update foreach key [array names threads::ns] { ${log}::debug "thread \"$key\" has id [set threads::ns($key)]" } } } namespace eval t { variable ns proc update {} { variable ns thread::send -head [set ns(main)] threads::update } proc ns {name} { variable ns set ns($name) } }