From: Mark Probert Date: 2004-01-20T11:10:00+09:00 Subject: threads, mutex, regexp and trouble Hello, rubyists. I have a bit of a problem suing threads. The program reads in a list of commands, then fires them off, via a telnet session, to another device, collecting data as it goes. On a one-by-one basis, everything is fine. the commands works, everything is peachy. The problem is I have to do 250+ sessions in a fairly limited time. Ok, use threads. The problem is that I appear to be occassional tromping a variable, and mutexing doesn't seem to work. Sometimes its ok, often it not. The code in question looks like: @threads << Thread.new(node) { |idx| nn, ip, usr, pwd = idx.split(/:/) begin bsn = BSN.new(ip, @timeout) # login and run each command if bsn.login then @cmds.each do |cmd| mutex.synchronize do puts "~~>> cmd=#{cmd}" bsn.cmd(cmd) end end bsn.logout else puts " !! Unable to reach node= #{nn}" end } @threads.each { |thr| thr.join } The problem is that the "cmd" variable seems to get mashed by a regexp match within bsn.cmd(). A good run looks like: .. Running 2 commands on 2 nodes. .. threading now ... ~~>> cmd=show subs isp=*_eeua ~~>> cmd=show subs isp=*_eeua A bad run looks like: .. Running 2 commands on 2 nodes. .. threading now ... ~~>> cmd=show subs isp=*_eeua ~~>> cmd=show subs isp=ebg1_eeua These commands come from a text file and the line is "show subs isp=*_eeua". The regexp does a match on data returned from the node. The first match for the first node is "ebg1_eeua". So, it seems that the regexp is confusing the thread. Any thoughts? Thanks, -mark.