Class: Netz::SafeArray
- Inherits:
-
Object
- Object
- Netz::SafeArray
- Defined in:
- lib/netz/safe_array.rb
Overview
a thread safe array class
Instance Method Summary collapse
- #delete(item) ⇒ Object
- #each(&block) ⇒ Object
- #empty? ⇒ Boolean
-
#initialize(items = []) ⇒ SafeArray
constructor
A new instance of SafeArray.
- #push(item) ⇒ Object (also: #<<)
- #size ⇒ Object
Constructor Details
#initialize(items = []) ⇒ SafeArray
Returns a new instance of SafeArray.
5 6 7 8 |
# File 'lib/netz/safe_array.rb', line 5 def initialize(items=[]) @items = items @mutex = Mutex.new end |
Instance Method Details
#delete(item) ⇒ Object
18 19 20 21 22 |
# File 'lib/netz/safe_array.rb', line 18 def delete(item) @mutex.synchronize { @items.delete item } end |
#each(&block) ⇒ Object
24 25 26 27 28 29 |
# File 'lib/netz/safe_array.rb', line 24 def each(&block) @mutex.synchronize { @items.each(&block) } self end |
#empty? ⇒ Boolean
31 32 33 34 35 |
# File 'lib/netz/safe_array.rb', line 31 def empty? @mutex.synchronize { @items.empty? } end |
#push(item) ⇒ Object Also known as: <<
10 11 12 13 14 15 |
# File 'lib/netz/safe_array.rb', line 10 def push(item) @mutex.synchronize { @items.push item } self end |
#size ⇒ Object
37 38 39 40 41 |
# File 'lib/netz/safe_array.rb', line 37 def size @mutex.synchronize { @items.size } end |