Class: ThreadSafeHash::ThreadSafeHash

Inherits:
Object
  • Object
show all
Defined in:
lib/process_monitoring/thread_safe_hash.rb

Instance Method Summary collapse

Constructor Details

#initializeThreadSafeHash

Returns a new instance of ThreadSafeHash.



4
5
6
7
# File 'lib/process_monitoring/thread_safe_hash.rb', line 4

def initialize()
  @hash_data = Hash.new()
  @mutex = Mutex.new
end

Instance Method Details

#cloneObject



32
33
34
35
36
# File 'lib/process_monitoring/thread_safe_hash.rb', line 32

def clone
  @mutex.synchronize do
    @hash_data.clone
  end
end

#get(key) ⇒ Object



20
21
22
23
24
# File 'lib/process_monitoring/thread_safe_hash.rb', line 20

def get(key)
  @mutex.synchronize do
    @hash_data[key]
  end
end

#inc(key) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/process_monitoring/thread_safe_hash.rb', line 9

def inc(key)
  @mutex.synchronize do
    value = @hash_data[key]
    if value.nil?
      @hash_data[key] = 1
    else
      @hash_data[key] = value + 1
    end
  end
end

#set(key, value) ⇒ Object



26
27
28
29
30
# File 'lib/process_monitoring/thread_safe_hash.rb', line 26

def set(key, value)
  @mutex.synchronize do
    @hash_data[key] = value
  end
end