Class: Concurrent::ThreadLocalVar

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

Overview

A ‘ThreadLocalVar` is a variable where the value is different for each thread. Each variable may have a default value, but when you modify the variable only the current thread will ever see that change.

Testing with ruby 2.1.2 Testing with Concurrent::MutexAtomicBoolean…

2.790000   0.000000   2.790000 (  2.791454)

Testing with Concurrent::CAtomicBoolean…

0.740000   0.000000   0.740000 (  0.740206)

Testing with jruby 1.9.3 Testing with Concurrent::MutexAtomicBoolean…

5.240000   2.520000   7.760000 (  3.683000)

Testing with Concurrent::JavaAtomicBoolean…

3.340000   0.010000   3.350000 (  0.855000)

Examples:

v = ThreadLocalVar.new(14)
v.value #=> 14
v.value = 2
v.value #=> 2
v = ThreadLocalVar.new(14)

t1 = Thread.new do
  v.value #=> 14
  v.value = 1
  v.value #=> 1
end

t2 = Thread.new do
  v.value #=> 14
  v.value = 2
  v.value #=> 2
end

v.value #=> 14

See Also:

Instance Method Summary collapse

Constructor Details

#initialize(default = nil) ⇒ Object

Creates a thread local variable.

Parameters:

  • default (Object) (defaults to: nil)

    the default value when otherwise unset



100
101
# File 'lib/concurrent/atomic/thread_local_var.rb', line 100

class ThreadLocalVar < ThreadLocalVarImplementation
end

Instance Method Details

#bind(value) { ... } ⇒ Object

Bind the given value to thread local storage during execution of the given block.

Parameters:

  • value (Object)

    the value to bind

Yields:

  • the operation to be performed with the bound variable

Returns:

  • (Object)

    the value



100
101
# File 'lib/concurrent/atomic/thread_local_var.rb', line 100

class ThreadLocalVar < ThreadLocalVarImplementation
end

#valueObject

Returns the value in the current thread’s copy of this thread-local variable.

Returns:

  • (Object)

    the current value



100
101
# File 'lib/concurrent/atomic/thread_local_var.rb', line 100

class ThreadLocalVar < ThreadLocalVarImplementation
end

#value=(value) ⇒ Object

Sets the current thread’s copy of this thread-local variable to the specified value.

Parameters:

  • value (Object)

    the value to set

Returns:

  • (Object)

    the new value



100
101
# File 'lib/concurrent/atomic/thread_local_var.rb', line 100

class ThreadLocalVar < ThreadLocalVarImplementation
end