Class: ThreadSafeHash::ThreadSafeHash
- Inherits:
-
Object
- Object
- ThreadSafeHash::ThreadSafeHash
- Defined in:
- lib/process_monitoring/thread_safe_hash.rb
Direct Known Subclasses
Instance Method Summary collapse
- #clone ⇒ Object
- #dec(key) ⇒ Object
- #get(key) ⇒ Object
- #inc(key) ⇒ Object
-
#initialize ⇒ ThreadSafeHash
constructor
A new instance of ThreadSafeHash.
- #set(key, value) ⇒ Object
Constructor Details
#initialize ⇒ ThreadSafeHash
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
#clone ⇒ Object
43 44 45 46 47 |
# File 'lib/process_monitoring/thread_safe_hash.rb', line 43 def clone @mutex.synchronize do @hash_data.clone end end |
#dec(key) ⇒ Object
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/process_monitoring/thread_safe_hash.rb', line 20 def dec(key) @mutex.synchronize do value = @hash_data[key] if value.nil? @hash_data[key] = -1 else @hash_data[key] = value - 1 end end end |
#get(key) ⇒ Object
31 32 33 34 35 |
# File 'lib/process_monitoring/thread_safe_hash.rb', line 31 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
37 38 39 40 41 |
# File 'lib/process_monitoring/thread_safe_hash.rb', line 37 def set(key, value) @mutex.synchronize do @hash_data[key] = value end end |