Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Semantic bug #49

Open
schneems opened this issue May 1, 2014 · 0 comments
Open

Semantic bug #49

schneems opened this issue May 1, 2014 · 0 comments

Comments

@schneems
Copy link

schneems commented May 1, 2014

Not technically a bug, but perhaps unexpected behavior. As the updating is the only atomic operation, items within the update block may not be correctly preserved. For example appending an element to an array would get done in parallel and values will get over-written:

require 'atomic'
array = Atomic.new([])

threads = []
def insert_into_array(array, value, threads)
  max = 100_000
  threads <<  Thread.new do
    max.times{
      array.update {|v|
        v << value
      }
    }
  end
end


insert_into_array(array, 1, threads)
insert_into_array(array, 2, threads)
insert_into_array(array, 3, threads)

threads.map(&:join)
puts array.value.size

When I run this in JRuby i get different values every time such as 294602.

Again, this is technically correct result, but by the semantics of update and the block syntax I would be lead to believe that update also acted like Mutex#synchronize so the results of the above would always return the same value.

Do you think there's anything we can do to make this experience better or this result more expected?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant