# Newb question: ThreadSafe::Array

**URL:** <https://www.ruby-forum.com/t/newb-question-threadsafe-array/237429>\
**Category:** JRuby\
**Created:** [November 13, 2013, 1:30am UTC](https://www.ruby-forum.com/t/newb-question-threadsafe-array/237429 "2013-11-13T01:30:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![musicdenotation](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ea666f/32.png) [@musicdenotation](https://www.ruby-forum.com/u/musicdenotation)\
**Post date:** [November 13, 2013, 1:30am UTC](https://www.ruby-forum.com/t/newb-question-threadsafe-array/237429/1 "2013-11-13T01:30:18Z")

</div>

I downloaded and was experimenting with the ThreadSafe::Array class, but  
am  
confused as to how the class is safer than the conventional array class.

In the following code sample, the result is never 0, as one might (or  
might  
not) expect it to be. What am I misunderstanding about the functioning  
of  
this class? Could someone illustrate a simple example where ThreadSafe  
functions, but a conventional array would fail?

Thanks in advance,  
Jon

require ‘thread\_safe’

N = ARGV[0].to\_i  
array = ThreadSafe::Array.new  
array[0] = 0  
threads = []

( 1…N ).each do |i|  
threads \<\< Thread.new {  
array[0] += 1  
array[0] -= 1  
}  
end

threads.each { |thread| thread.join }

puts array[0]

---

<div class="post-metadata">

**Author:** ![jonbaer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/54ee81/32.png) [@jonbaer](https://www.ruby-forum.com/u/jonbaer)\
**Post date:** [November 13, 2013, 4:04am UTC](https://www.ruby-forum.com/t/newb-question-threadsafe-array/237429/2 "2013-11-13T04:04:54Z")

</div>

I may be wrong about this but the “+=” line should not be an atomic  
operation. Multiple threads may read the value in sequence and then  
mutate the values in sequence. What I think you may be looking for is an  
atomic increment function.

## Thanks, Ariel

Sent from my mobile device. Please excuse any errors.

---

<div class="post-metadata">

**Author:** ![jonbaer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/54ee81/32.png) [@jonbaer](https://www.ruby-forum.com/u/jonbaer)\
**Post date:** [November 13, 2013, 5:00am UTC](https://www.ruby-forum.com/t/newb-question-threadsafe-array/237429/3 "2013-11-13T05:00:59Z")

</div>

This should do it:

data = [] # or ThreadSafe::Array.new  
threads = []  
16.times do |m|  
threads \<\< Thread.new do  
16.times do |n|  
data \<\< m \* n  
end  
end  
end

threads.map(&:join)  
puts “size =\> #{data.size}”

I wrote a blog a while back describing why:

> **[Why JRuby Arrays are not Threadsafe](http://deployingjruby.blogspot.com/2013/07/why-jruby-arrays-are-not-threadsafe.html)**
>
> Run JRuby-Lint against one of your Rails projects and you'll probably see this message: It's a warning that the statement below is a po...

---

<div class="post-metadata">

**Author:** ![jonbaer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/54ee81/32.png) [@jonbaer](https://www.ruby-forum.com/u/jonbaer)\
**Post date:** [November 13, 2013, 4:42am UTC](https://www.ruby-forum.com/t/newb-question-threadsafe-array/237429/4 "2013-11-13T04:42:17Z")

</div>

Ariel, that thought occurred to me, and I think you must be right. What  
I’m still a bit confused by is that I haven’t been able to create a code  
sample where the standard Array fails, but ThreadSafe::Array performs as  
the user intended in multi-threaded code. My previous example failed  
for  
both Array and ThreadSafe::Array. The example below works for both,  
although one might think it would fail with Array.  
Can anyone out there demonstrate an example where Array fails and  
ThreadSafe::Array behaves as the user intended?

Cheers,  
Jon

N = ARGV[0].to\_i  
array = []  
threads = []

( 1…N ).each do |i|  
threads \<\< Thread.new {  
array \<\< 1  
}  
end

threads.each { |thread| thread.join }

puts array.length
