Skip to content

Allow ThreadLocalVar to be initialized with a block #557

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 3, 2016
Merged

Allow ThreadLocalVar to be initialized with a block #557

merged 3 commits into from
Aug 3, 2016

Conversation

ivoanjo
Copy link
Contributor

@ivoanjo ivoanjo commented Jul 23, 2016

ThreadLocalVars can now receive a block in the #initialize method, which will be called to initialize the value on each thread.

This allows default initialization to work with non-value types, where
each thread gets its own copy:

v = ThreadLocalVar.new { {} }
h1 = Thread.new { v.value }.value
h2 = Thread.new { v.value }.value

h1.equal? h2 # -> false

ivoanjo added 3 commits July 23, 2016 08:29
ThreadLocalVars can now receive a block in the #initialize method,
which will be called to initialize the value on each thread.

This allows default initialization to work with non-value types, where
each thread gets its own copy:

    v = ThreadLocalVar.new { {} }
    h1 = Thread.new { v.value }.value
    h2 = Thread.new { v.value }.value

    h1.equal? h2 # -> false
@jdantonio
Copy link
Member

Thank you so much @ivoanjo! This is fantastic work. I want to pull the code locally and experiment with it before I merge it in, but I expect I will merge it without changes.

@ivoanjo
Copy link
Contributor Author

ivoanjo commented Jul 29, 2016

Thanks! I was inspired a bit by Hash which has a similar API (although without saving the default value after getting it from the block), and it really feels much more natural and high-level to use.

@jdantonio jdantonio merged commit 58cec34 into ruby-concurrency:master Aug 3, 2016
@jdantonio jdantonio added the enhancement Adding features, adding tests, improving documentation. label Aug 3, 2016
@jdantonio jdantonio added this to the 1.0.3 milestone Aug 3, 2016
@ivoanjo
Copy link
Contributor Author

ivoanjo commented Aug 3, 2016

Awesome!

@ivoanjo ivoanjo deleted the thread-local-block-initialization branch August 3, 2016 21:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Adding features, adding tests, improving documentation.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants