I’m trying to use the Atomic and Threadify libraries, but I’m either
misunderstanding something somewhere or they don’t play nice together.
The output of the one liner below should be 166650, but the Atomic
updates look like they are being randomly missed sometimes:
jruby -rthreadify -ratomic -e
‘t=Atomic.new(0);(0…100).to_a.threadify(24){|n| n.times{|x|
t.update{|v|v+x}}};p t.value’
166727
jruby -rthreadify -ratomic -e
‘t=Atomic.new(0);(0…100).to_a.threadify(24){|n| n.times{|x|
t.update{|v|v+x}}};p t.value’
167145
jruby -rthreadify -ratomic -e
‘t=Atomic.new(0);(0…100).to_a.threadify(24){|n| n.times{|x|
t.update{|v|v+x}}};p t.value’
166814
jruby -rthreadify -ratomic -e
‘t=Atomic.new(0);(0…100).to_a.threadify(1){|n| n.times{|x|
t.update{|v|v+x}}};p t.value’
166650
Doing it without Threadify also seems to work:
jruby -ratomic -e
‘t=Atomic.new(0);a=(0…100).to_a;th=[];a.each{|n|th<<Thread.new(n){n.times{|x|t.update{|v|v+x}}}};th.each{|ths|
ths.join};p t.value’
166650
–
Alex G.