Threadsafety

Makes things threadsafe.

Installation

Add this line to your application's Gemfile:

gem 'threadsafety'

And then execute:

$ bundle

Or install it yourself as:

$ gem install threadsafety

Usage

Use it via mixin or subclass:

require "threadsafety"

class MyObject < ThreadsafeObject
  attr_accessor :something
  # ...
end

class MySecondObject
  include Threadsafety
  attr_accessor :something
  # ...
end

It overrides the attr_writer and attr_accessor methods on Module (and therefore Class due to inheritance) to create threadsafe setters.

If you need to write your own threadsafe code, you can use the method Threadsafety#threadsafe:

theadsafe do
  # stuff that needs to be threadsafe
end

Threadsafety#threadsafe returns whatever the block you pass it returns. You can also nest calls to Threadsafety#threadsafe on the same thread, unlike Mutex#synchronize. It's defined on global scope, class scope, and object scope, each using their own mutex.

Contributing

  1. Fork it ( https://github.com/[my-github-username]/threadsafety/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request