Class: Netz::SafeArray

Inherits:
Object
  • Object
show all
Defined in:
lib/netz/safe_array.rb

Overview

a thread safe array class

Instance Method Summary collapse

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

Returns:

  • (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

#sizeObject



37
38
39
40
41
# File 'lib/netz/safe_array.rb', line 37

def size
  @mutex.synchronize {
    @items.size
  }
end