Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Synchronized access to class and instance atttributes across threads, including lazy initialization of attributes

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 examples
Octocat-spinner-32 lib
Octocat-spinner-32 nbproject
Octocat-spinner-32 test
Octocat-spinner-32 .gitignore
Octocat-spinner-32 .rvmrc
Octocat-spinner-32 Gemfile
Octocat-spinner-32 LICENSE.txt
Octocat-spinner-32 README.md
Octocat-spinner-32 Rakefile
README.md

sync_attr

Thread-safe Ruby class variables with lazy loaded default values and initializers

Introduction

When working in a multithreaded environment it is important to ensure that any attributes that are shared across threads are properly protected to ensure that inconsistent data is not created.

For example, without sync_attr if two threads attempt to write to the same attribute at the same time it is not deterministic what the results will be. This condition is made worse when two threads attempt to initialize class variables at the same time that could take a second or longer to complete.

Features

  • Adds thread-safe accessors for class attributes
  • Allows shared read access to class and instance attributes. This allows multiple threads to read the attribute, but will block all reads and writes whilst the attribute is being modified.
  • Prevents attributes from being read while it is being updated or initialized for the first time.
  • Thread-safe attribute lazy initialization Lazy initialization allows class attributes to be loaded only when first read. As a result it's value can be read for the first time from a database or config file once and only when needed.
  • Avoids having to create yet another Rails initializer
  • Avoids costly startup initialization when the initialized data may never be accessed For example when Rake tasks are run, they may not need access to everything in the Rails environment
  • Not dependent on Rails

Synchronized Class Attribute example

require 'sync_attr'

# Sample class with lazy initialized Synchronized Class Attributes
class Person
  include SyncAttr

  # Thread safe Class Attribute reader for name
  # with a default value
  # Ideal for when name is loaded after startup from a database or config file
  sync_cattr_reader :name do
    "Joe Bloggs"
  end

  # Thread safe Class Attribute reader and writer for age
  # with a default value
  sync_cattr_accessor :age do
    21
  end
end

puts "The person is #{Person.name} with age #{Person.age}"

Person.age = 22
puts "The person is #{Person.name} now has age #{Person.age}"

Person.age = Proc.new {|age| age += 1 }
puts "The person is #{Person.name} now has age #{Person.age}"

Synchronized Instance Attribute example

require 'sync_attr'

# Sample class with lazy initialized Synchronized Class Attributes
class Person
  include SyncAttr

  # Thread safe Attribute reader for name
  # with a default value
  sync_attr_reader :name do
    "Joe Bloggs"
  end

  # Thread safe Attribute reader and writer for age
  # with a default value
  sync_attr_accessor :age do
    21
  end
end

person = Person.new
puts "The person is #{person.name} with age #{person.age}"

person.age = 22
puts "The person is #{person.name} now has age #{person.age}"

person.age = Proc.new {|age| age += 1 }
puts "The person is #{person.name} now has age #{person.age}"

Install

gem install sync_attr

Meta

This project uses Semantic Versioning.

Authors

Reid Morrison :: reidmo@gmail.com :: @reidmorrison

License

Copyright 2012 Clarity Services, Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Something went wrong with that request. Please try again.