123456789_123456789_123456789_123456789_123456789_

Class: Concurrent::AtomicBoolean

Relationships & Source Files
Inherits: Concurrent::AtomicBooleanImplementation
Defined in: lib/concurrent-ruby/concurrent/atomic/atomic_boolean.rb

Overview

A boolean value that can be updated atomically. Reads and writes to an atomic boolean and thread-safe and guaranteed to succeed. Reads and writes may block briefly but no explicit locking is required.

## Thread-safe Variable Classes

Each of the thread-safe variable classes is designed to solve a different problem. In general:

  • *Agent:* Shared, mutable variable providing independent, uncoordinated, asynchronous change of individual values. Best used when the value will undergo frequent, complex updates. Suitable when the result of an update does not need to be known immediately.

  • *Atom:* Shared, mutable variable providing independent, uncoordinated, synchronous change of individual values. Best used when the value will undergo frequent reads but only occasional, though complex, updates. Suitable when the result of an update must be known immediately.

  • *AtomicReference:* A simple object reference that can be updated atomically. Updates are synchronous but fast. Best used when updates a simple set operations. Not suitable when updates are complex. AtomicBoolean and AtomicFixnum are similar but optimized for the given data type.

  • *Exchanger:* Shared, stateless synchronization point. Used when two or more threads need to exchange data. The threads will pair then block on each other until the exchange is complete.

  • *MVar:* Shared synchronization point. Used when one thread must give a value to another, which must take the value. The threads will block on each other until the exchange is complete.

  • *ThreadLocalVar:* Shared, mutable, isolated variable which holds a different value for each thread which has access. Often used as an instance variable in objects which must maintain different state for different threads.

  • *TVar:* Shared, mutable variables which provide coordinated, synchronous, change of many stated. Used when multiple value must change together, in an all-or-nothing transaction.

Performance:

“‘ Testing with ruby 2.1.2 Testing with MutexAtomicBoolean

2.790000   0.000000   2.790000 (  2.791454)

Testing with CAtomicBoolean

0.740000   0.000000   0.740000 (  0.740206)

Testing with jruby 1.9.3 Testing with MutexAtomicBoolean

5.240000   2.520000   7.760000 (  3.683000)

Testing with Concurrent::JavaAtomicBoolean

3.340000   0.010000   3.350000 (  0.855000)

“‘

Class Method Summary

Instance Method Summary

Constructor Details

.new(initial = false)

Creates a new AtomicBoolean with the given initial value.

Parameters:

  • initial (Boolean) (defaults to: false)

    the initial value

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/atomic/atomic_boolean.rb', line 119

class AtomicBoolean < AtomicBooleanImplementation
  # @return [String] Short string representation.
  def to_s
    format '%s value:%s>', super[0..-2], value
  end

  alias_method :inspect, :to_s
end

Instance Method Details

#false?Boolean

Is the current value false

Returns:

  • (Boolean)

    true if the current value is false, else false

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/atomic/atomic_boolean.rb', line 119

class AtomicBoolean < AtomicBooleanImplementation
  # @return [String] Short string representation.
  def to_s
    format '%s value:%s>', super[0..-2], value
  end

  alias_method :inspect, :to_s
end

#inspect

Alias for #to_s.

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/atomic/atomic_boolean.rb', line 125

alias_method :inspect, :to_s

#make_falseBoolean

Explicitly sets the value to false.

Returns:

  • (Boolean)

    true if value has changed, otherwise false

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/atomic/atomic_boolean.rb', line 119

class AtomicBoolean < AtomicBooleanImplementation
  # @return [String] Short string representation.
  def to_s
    format '%s value:%s>', super[0..-2], value
  end

  alias_method :inspect, :to_s
end

#make_trueBoolean

Explicitly sets the value to true.

Returns:

  • (Boolean)

    true if value has changed, otherwise false

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/atomic/atomic_boolean.rb', line 119

class AtomicBoolean < AtomicBooleanImplementation
  # @return [String] Short string representation.
  def to_s
    format '%s value:%s>', super[0..-2], value
  end

  alias_method :inspect, :to_s
end

#to_sString Also known as: #inspect

Returns:

  • (String)

    Short string representation.

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/atomic/atomic_boolean.rb', line 121

def to_s
  format '%s value:%s>', super[0..-2], value
end

#true?Boolean

Is the current value true

Returns:

  • (Boolean)

    true if the current value is true, else false

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/atomic/atomic_boolean.rb', line 119

class AtomicBoolean < AtomicBooleanImplementation
  # @return [String] Short string representation.
  def to_s
    format '%s value:%s>', super[0..-2], value
  end

  alias_method :inspect, :to_s
end

#valueBoolean

Retrieves the current Boolean value.

Returns:

  • (Boolean)

    the current value

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/atomic/atomic_boolean.rb', line 119

class AtomicBoolean < AtomicBooleanImplementation
  # @return [String] Short string representation.
  def to_s
    format '%s value:%s>', super[0..-2], value
  end

  alias_method :inspect, :to_s
end

#value=(value) ⇒ Boolean

Explicitly sets the value.

Parameters:

  • value (Boolean)

    the new value to be set

Returns:

  • (Boolean)

    the current value

[ GitHub ]

  
# File 'lib/concurrent-ruby/concurrent/atomic/atomic_boolean.rb', line 119

class AtomicBoolean < AtomicBooleanImplementation
  # @return [String] Short string representation.
  def to_s
    format '%s value:%s>', super[0..-2], value
  end

  alias_method :inspect, :to_s
end