"Mark Probert" <probertm / nospam-acm.org> schrieb im Newsbeitrag
news:Xns947ED077718CDprobertmnospamacmorg / 206.172.150.14...
> "Robert Klemme" <bob.news / gmx.net> wrote:
> >
> > Is it guaranteed that all threads use the same instance via 'mutex',
> > i.e. does synchronization work properly?  Or maybe you use gsub!
> > instead of gsub (which you should since apparently all threads share
> > @cmds).
> >
>
> Thank you very much for your input.  It is was the @cmds that was in
fact
> getting overwritten.  I changed how I processed it, to rereading the
file
> for each thread, and the problem went away.

Rereading the file is a bad solution (performance wise).  Why don't you
just copy it?  Depending on whether only the reference @cmd was
overwritte, the collection changed or even elements were changed one of
these three options is most appropriate (in order):

@threads << Thread.new(node, @cmds) { |idx, commands|
  nn, ip, usr, pwd = idx.split(/:/)

  begin
    bsn = BSN.new(ip, @timeout)

    # login and run each command
    if bsn.login then
      begin
        commands.each do |cmd|
          mutex.synchronize do
            puts "~~>> cmd=#{cmd}"
            bsn.cmd(cmd)
          end
        end
      ensure
        bsn.logout
      end
    else
      $stderr.puts " !! Unable to reach node= #{nn}"
    end
  rescue Exception => e
    $stderr.puts e
  end
}

@threads << Thread.new(node, @cmds.dup) { |idx, commands|
  nn, ip, usr, pwd = idx.split(/:/)
....
end


@threads << Thread.new(node, Marshal.load(Marshal.dump(@cmds))) { |idx,
commands|
  nn, ip, usr, pwd = idx.split(/:/)
....
end

> Thanks also for the exception handling suggestions.  Implemented!

Yeah, that's an often seen problem: exceptions that terminate threads are
normally not shown so that threads seem to die silently.

Regards

    robert