# Atomic 0.0.1 - An atomic reference for Ruby

**URL:** <https://www.ruby-forum.com/t/atomic-0-0-1-an-atomic-reference-for-ruby/189803>\
**Category:** Ruby\
**Created:** [June 8, 2010, 6:07am UTC](https://www.ruby-forum.com/t/atomic-0-0-1-an-atomic-reference-for-ruby/189803 "2010-06-08T06:07:06Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![headius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/7c8e57/32.png) [@headius](https://www.ruby-forum.com/u/headius)\
**Post date:** [June 8, 2010, 6:07am UTC](https://www.ruby-forum.com/t/atomic-0-0-1-an-atomic-reference-for-ruby/189803/1 "2010-06-08T06:07:06Z")

</div>

atomic: An atomic reference implementation for JRuby and green or  
GIL-threaded  
Ruby implementations (MRI 1.8/1.9, Rubinius)

== Summary ==

This library provides:

- an Atomic class that guarantees atomic updates to its contained value

The Atomic class provides accessors for the contained “value” plus two  
update  
methods:

- update will run the provided block, passing the current value and  
replacing  
it with the block result iff the value has not been changed in the  
mean time.  
It may run the block repeatedly if there are other concurrent updates  
in  
progress.
- try\_update will run the provided block, passing the current value and  
replacing it with the block result. If the value changes before the  
update  
can happen, it will throw Atomic::ConcurrentUpdateError.

The atomic repository is at [GitHub - headius/ruby-atomic: Atomic reference implementation for Ruby](http://github.com/headius/ruby-atomic).

== Usage ==

# gem install atomic

require ‘atomic’

my\_atomic = Atomic.new(0)  
my\_atomic.update {|v| v + 1}  
begin  
my\_atomic.try\_update {|v| v + 1}  
rescue Atomic::ConcurrentUpdateError =\> cue

# deal with it (retry, propagate, etc)

end

---

<div class="post-metadata">

**Author:** ![headius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/7c8e57/32.png) [@headius](https://www.ruby-forum.com/u/headius)\
**Post date:** [June 8, 2010, 6:31am UTC](https://www.ruby-forum.com/t/atomic-0-0-1-an-atomic-reference-for-ruby/189803/2 "2010-06-08T06:31:07Z")

</div>

And just pushed a minor update in 0.0.2 🙂

On Tue, Jun 8, 2010 at 12:06 AM, Charles Oliver N.

---

<div class="post-metadata">

**Author:** ![headius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/7c8e57/32.png) [@headius](https://www.ruby-forum.com/u/headius)\
**Post date:** [June 8, 2010, 10:08am UTC](https://www.ruby-forum.com/t/atomic-0-0-1-an-atomic-reference-for-ruby/189803/3 "2010-06-08T10:08:52Z")

</div>

On Tue, Jun 8, 2010 at 6:06 AM, Charles Oliver N.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:  
What a great idea!  
Continue like that and we will be able to program Clojure with Ruby  
Syntax.  
Seriously this is a great concept, as Clojure is a great language. I  
guess that following Ola’s ideas about polyglot programming Ruby and  
Clojure would indeed make a beautiful couple.  
Many kudos!

Cheers  
Robert

---

<div class="post-metadata">

**Author:** ![headius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/7c8e57/32.png) [@headius](https://www.ruby-forum.com/u/headius)\
**Post date:** [June 8, 2010, 2:22pm UTC](https://www.ruby-forum.com/t/atomic-0-0-1-an-atomic-reference-for-ruby/189803/4 "2010-06-08T14:22:24Z")

</div>

It’s possible to do some of Clojure’s magic right now with my “Cloby”  
library, which adds a Clojure::Object supertype you can use to get  
transactional semantics for Ruby instance variables:

`
require 'clojure'
class MyClojureObj < Clojure::Object
def initialize
dosync { @foo = ‘foo’ }
end
attr_accessor :foo
end
obj = MyClojureObj.new
puts "obj.foo = " + obj.foo
begin
puts “Setting obj.foo to ‘bar’”
obj.foo = ‘bar’
rescue ConcurrencyError
puts “Oops, need a transaction”
end
puts “Trying again with a transaction”
dosync { obj.foo = ‘bar’ }
puts “Success”
`

`puts "obj.foo = " + obj.foo
`

This is in the “cloby” library I’ve never released, at  
[GitHub - headius/cloby: A Clojure Ref/STM plugin for JRuby](http://github.com/headius/cloby).

---

<div class="post-metadata">

**Author:** ![headius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/7c8e57/32.png) [@headius](https://www.ruby-forum.com/u/headius)\
**Post date:** [June 8, 2010, 3:05pm UTC](https://www.ruby-forum.com/t/atomic-0-0-1-an-atomic-reference-for-ruby/189803/5 "2010-06-08T15:05:09Z")

</div>

On Tue, Jun 8, 2010 at 2:21 PM, Charles Oliver N.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> This is in the “cloby” library I’ve never released, at  
> [GitHub - headius/cloby: A Clojure Ref/STM plugin for JRuby](http://github.com/headius/cloby).  
> Just too good to be true 😛  
> Thx for your work.  
> Cheers  
> R.

---

<div class="post-metadata">

**Author:** ![headius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/7c8e57/32.png) [@headius](https://www.ruby-forum.com/u/headius)\
**Post date:** [June 8, 2010, 2:23pm UTC](https://www.ruby-forum.com/t/atomic-0-0-1-an-atomic-reference-for-ruby/189803/6 "2010-06-08T14:23:00Z")

</div>

And now a 0.0.3 that adds “swap”, fixes “update” return value to be  
the new value, and uses a small extension for JRuby for perf and to  
preserve value identity.

On Tue, Jun 8, 2010 at 4:29 AM, Charles Oliver N.
