"Rasputin" <rasputnik / hellooperator.net> schrieb im Newsbeitrag
news:20040121170047.GA20259 / lb.tenfour...
>
> I decided its high time I tried to learn threads again,
> and my best chance is to do it in Ruby.
>
> But the scheduler doesnt seem to want to run any threads
> until the previous ones have finished:

That's just an accidental thing.  Your threads probably don't do enough to
trigger a context switch.

>
> ------------------8<------------------------
>
> 0rasputin@lb:rasputin$ cat freds.rb
> symbols = %w( = ^ V < > @ # ! )
> threads = []
> string = ""
>
> symbols.each { |symbol|
>         threads << Thread.new(symbol) do |sym|
>                 9.times { string << sym }
>         end
> }
>
> threads.each { |w| threads.join }
> puts string
> 0rasputin@lb:rasputin$ ruby freds.rb
> =========^^^^^^^^^VVVVVVVVV<<<<<<<<<>>>>>>>>>@@@@@@@@@#########!!!!!!!!!
> 0rasputin@lb:rasputin$
>

You need to synchronize access to the shared resource 'string'.  And you
joined on the wrong variable:

Robert@Babelfish2 ~/test
$ ruby t.rb
=========^^^^^^^^^VVVVVVVVV<<<<<<<<<>>>#########@@@@@@@@@>>!!!!!!!!!>>>>

Robert@Babelfish2 ~/test
$ cat t.rb
require 'Thread'

symbols = %w( = ^ V < > @ # ! )
threads = []
string = ""
mutex = Mutex.new

symbols.each { |symbol|
        threads << Thread.new(symbol) do |sym|
                9.times { mutex.synchronize { string << sym } }
        end
}

threads.each { |w| w.join }
puts string


Robert@Babelfish2 ~/test
$

Regards

    robert


> ------------------8<------------------------
>
> The pickaxe example at:
> http://www.rubycentral.com/book/tut_threads.html#UA
> , which is almost identical, seems to timeslice ok.
>
> And if I scatter some magic Thread.pass dust in there it behaves itself.
> Is it just that the threads exit too soon to get suspended?
>
> This is on NetBSD 1.6Z with Ruby 1.6.8 and todays CVS.
>
>
>
> -- 
> $100 invested at 7% interest for 100 years will become $100,000, at
> which time it will be worth absolutely nothing.
> -- Lazarus Long, "Time Enough for Love"
> Rasputin :: Jack of All Trades - Master of Nuns
>
>