mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
AS guide: documents Module#synchronize
This commit is contained in:
parent
b3e43a6462
commit
81e779a019
1 changed files with 24 additions and 0 deletions
|
@ -791,6 +791,30 @@ WARNING: This method is exact if running under Ruby 1.9. In previous versions it
|
|||
|
||||
NOTE: Defined in +active_support/core_ext/module/introspection.rb+.
|
||||
|
||||
h4. Synchronization
|
||||
|
||||
The +synchronize+ macro declares a method to be synchronized:
|
||||
|
||||
<ruby>
|
||||
class Counter
|
||||
@@mutex = Mutex.new
|
||||
attr_reader :value
|
||||
|
||||
def initialize
|
||||
@value = 0
|
||||
end
|
||||
|
||||
def incr
|
||||
@value += 1 # non-atomic
|
||||
end
|
||||
synchronize :incr, :with => '@@mutex'
|
||||
end
|
||||
</ruby>
|
||||
|
||||
The method receives the name of an action, and a +:with+ option with code. The code is evaluated in the context of the receiver each time the method is invoked, and it should evaluate to a +Mutex+ instance or any other object that responds to +synchronize+ and accepts a block.
|
||||
|
||||
NOTE: Defined in +active_support/core_ext/module/synchronization.rb+.
|
||||
|
||||
h3. Extensions to +Class+
|
||||
|
||||
h4. Class Attributes
|
||||
|
|
Loading…
Reference in a new issue