Concurrent::ThreadLocalVar โ A thread-local variable with perโthread values.
| Use Case | Command | Description |
|---|---|---|
| Create a threadโlocal variable with default value | ThreadLocalVar.new(14) | Initializes a new variable; each thread sees the default value unless modified. |
| Read current threadโs value | v.value | Returns the value for the calling thread. |
| Set current threadโs value | v.value = 2 | Changes the value only for the current thread; other threads are unaffected. |
| Demonstrate isolation across threads | Thread.new { v.value = 1 } | Each thread can independently modify its own copy of the variable. |
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.
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
Generated by phpman v4.9.26-1-g511901d · Markdown · JSON · MCP Author: Che Dong Under GNU General Public License
2026-07-28 01:04 @216.73.217.25
CrawledBy Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)
Enhanced by LLM: deepseek-v4-flash / taotoken.net / www.chedong.com - original format