Last active
August 29, 2015 13:56
-
-
Save azet/8864785 to your computer and use it in GitHub Desktop.
threading in Tcl with package "Thread"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.