Skip to content

Instantly share code, notes, and snippets.

@azet
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save azet/8864785 to your computer and use it in GitHub Desktop.
Save azet/8864785 to your computer and use it in GitHub Desktop.
threading in Tcl with package "Thread"
package require Thread
set network "192.168.0"
set threads 8
# create $threads number of threads
for {set t 0} {$t < $threads} {incr t} {
set thread_id [thread::create {
puts " >> thread [thread::id] started."
proc example_function {value} {
do_stuff_with $value
}
thread::wait
puts " >> ending thread [thread::id]."
}]
lappend thread_list $thread_id
}
# main loop
for {set i 1} {$i < 254} {incr i} {
set value "$network.$i"
foreach thread $thread_list {
puts " >> sending job for ip: $ip to thread: $thread."
thread::send $thread {
[example_function $value]
} result
}
vwait result
}
exit 0
#EOF
@azet
Copy link
Author

azet commented Feb 7, 2014

original intention was to use "example_function" (as it's called now) with expect(1) - but due to expect not being thread safe (https://groups.google.com/forum/#!msg/comp.lang.tcl/r8g7QNVcanY/GB83pakr24AJ), this just does not work. :(

so here. have some example code for threaded Tcl.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment