Class: Concurrent::Condition Deprecated

Inherits:
Object
  • Object
show all
Defined in:
lib/concurrent/atomic/condition.rb

Overview

Deprecated.

Condition is a better implementation of standard Ruby ConditionVariable. The biggest difference is the wait return value: Condition#wait returns Condition::Result which make possible to know if waiting thread has been woken up by an another thread (using #signal or #broadcast) or due to timeout.

Every #wait must be guarded by a locked Mutex or a ThreadError will be risen. Although it's not mandatory, it's recommended to call also #signal and #broadcast within the same mutex

Defined Under Namespace

Classes: Result

Instance Method Summary collapse

Constructor Details

#initialize ⇒ Condition

Returns a new instance of Condition.



43
44
45
46
# File 'lib/concurrent/atomic/condition.rb', line 43

def initialize
  deprecated 'Will be replaced with Synchronization::Object in v1.0.'
  @condition = ConditionVariable.new
end

Instance Method Details

#broadcast ⇒ true

Wakes up all waiting threads

Returns:

  • (true)


73
74
75
76
# File 'lib/concurrent/atomic/condition.rb', line 73

def broadcast
  @condition.broadcast
  true
end

#signal ⇒ true

Wakes up a waiting thread

Returns:

  • (true)


66
67
68
69
# File 'lib/concurrent/atomic/condition.rb', line 66

def signal
  @condition.signal
  true
end

#wait(mutex, timeout = nil) ⇒ Result

Note:

Time calculations one all platforms and languages are sensitive to changes to the system clock. To alleviate the potential problems associated with changing the system clock while an application is running, most modern operating systems provide a monotonic clock that operates independently of the system clock. A monotonic clock cannot be used to determine human-friendly clock times. A monotonic clock is used exclusively for calculating time intervals. Not all Ruby platforms provide access to an operating system monotonic clock. On these platforms a pure-Ruby monotonic clock will be used as a fallback. An operating system monotonic clock is both faster and more reliable than the pure-Ruby implementation. The pure-Ruby implementation should be fast and reliable enough for most non-realtime operations. At this time the common Ruby platforms that provide access to an operating system monotonic clock are MRI 2.1 and above and JRuby (all versions).

Parameters:

  • mutex (Mutex) —

    the locked mutex guarding the wait

  • timeout (Object) (defaults to: nil) —

    nil means no timeout

Returns:

See Also:



53
54
55
56
57
58
59
60
61
62
# File 'lib/concurrent/atomic/condition.rb', line 53

def wait(mutex, timeout = nil)
  start_time = Concurrent.monotonic_time
  @condition.wait(mutex, timeout)

  if timeout.nil?
    Result.new(nil)
  else
    Result.new(start_time + timeout - Concurrent.monotonic_time)
  end
end