J
Jean G.
Hello,
$ cat count_threads.rb
class Counter
attr_reader :count
def initialize
@count = 0
end
def tick
@count += 1
end
end
c = Counter.new
t1 = Thread.new { 10000.times { c.tick } }
t2 = Thread.new { 10000.times { c.tick } }
t1.join
t2.join
puts c.count
$ ruby count_threads.rb
20000
$ ruby count_threads.rb
20000
$ ruby count_threads.rb
20000
My ruby version is 1.9, running under Linux-2.6.31 kernel.
For the code above, though we know the @count isn't safe for sharing.
But the result is always correct.
So can we say ruby-1.9 behaves better on threads than 1.8?
Thanks.
$ cat count_threads.rb
class Counter
attr_reader :count
def initialize
@count = 0
end
def tick
@count += 1
end
end
c = Counter.new
t1 = Thread.new { 10000.times { c.tick } }
t2 = Thread.new { 10000.times { c.tick } }
t1.join
t2.join
puts c.count
$ ruby count_threads.rb
20000
$ ruby count_threads.rb
20000
$ ruby count_threads.rb
20000
My ruby version is 1.9, running under Linux-2.6.31 kernel.
For the code above, though we know the @count isn't safe for sharing.
But the result is always correct.
So can we say ruby-1.9 behaves better on threads than 1.8?
Thanks.