Class List Method List File List

# Class: Concurrent::ThreadLocalVar

Inherits:
  ThreadLocalVarImplementation
  - Object
  - Concurrent::ThreadLocalVar
  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:

- Java ThreadLocal

## Instance Method Summary (collapse)

- - (Object) bind(value) { ... }

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

- - (undocumented) initialize(default = nil) constructor

  Creates a thread local variable.

- - (Object) value

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

- - (Object) value=(value)

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

## Constructor Details

### - (`undocumented`) initialize(default = nil)

Creates a thread local variable.

Parameters:

- default (`Object`) (defaults to: `nil`) —

  the default value when otherwise unset

- default_block (`Proc`) —

  Optional block that gets called to obtain the default value for each thread

    102
    103

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

    class ThreadLocalVar < ThreadLocalVarImplementation
    end

## Instance Method Details

### - (`Object`) bind(value) { ... }

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

    102
    103

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

    class ThreadLocalVar < ThreadLocalVarImplementation
    end

### - (`Object`) value

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

Returns:

- (`Object`) —

  the current value

    102
    103

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

    class ThreadLocalVar < ThreadLocalVarImplementation
    end

### - (`Object`) value=(value)

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

    102
    103

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

    class ThreadLocalVar < ThreadLocalVarImplementation
    end