-
Notifications
You must be signed in to change notification settings - Fork 24
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
thread-safety in MRI too? #15
Comments
MRI doesn't release GIL while in C code, all of the Array and Hash methods are pure C code on MRI, therefore what you describe can't actually happen. Also, if you are looking for a thread safe Hash, I'm certain you will be better served by ThreadSafe::Cache. |
@thedarkone Can you say more about this? What are the benefits of using |
@sferik
|
Huh, what, if any, are the situations where you'd want to use a TS::Hash instead of a TS::Cache? It sounds from that description like TS::Cache is just plain better. This would be good stuff for the README; from the README people might assume TS::Hash is the way to go, and not even know about TS::Cache. |
@jrochkind 👍 I was just about to suggest the same thing. I naïvely used @thedarkone One question: the following change is raising def define_memoize_method(method)
method_name = method.name.to_sym
undef_method(method_name)
define_method(method_name) do
- memory.fetch(method_name) do
- value = method.bind(self).call
- store_memory(method_name, value)
+ memory.compute_if_absent(method_name) do
+ method.bind(self).call
end
end
end
-def store_memory(name, value)
- memory[name] = value
-end
def memory
@memory ||= ThreadSafe::Cache.new
end Is that a bug or am I doing something wrong? |
Here's my question... if you would never really want ThreadSafe::Hash instead of ThreadSafe::Cache, why don't they use the same impl? Also note some Ruby 2.1 features I'm pushing for that would make a dumb synchronized Hash (and Array) a bit less necessary: |
@headius That’s a good question. Is there anything we can do to help these features make it into Ruby 2.1? In the long term, that’s more desirable than maintaining this library forever. |
Something this big in 2.1 might be tough, even if matz wasn't already opposed to adding more collection classes. I've tried to find something that matz will like, but have been unsuccessful. My issue #8556 attempts to add a standard mechanism for creating synchronization-wrapped objects, which would make ThreadSafe::Hash mostly unnecessary (though probably faster until we do a fast SynchronizedDelegate in JRuby), so that's a step forward. And 8961 will provide a way for users to start building data structures that have synchronization built in from the start in Java style. |
@sferik unfortunately, check-then-act methods cannot be recursive (ie: they cannot modify the same In your use case you might not care about computing some values multiple times, so you can just use @headius @jrochkind Ruby hashes are now insertion ordered, this has some serious performance implications for a concurrent data structure (I even dislike this in plain old Ruby hashes). That is the main reason for @headius as for the Ruby concurrency enhancements something like this: https://bugs.ruby-lang.org/issues/8259 beats pretty much everything 😋. BTW: I'm still waiting for JRuby to get fast enough for |
@thedarkone Thank you for explaining the insertion ordering issue. That's a very good point. As for https://bugs.ruby-lang.org/issues/8259, I've commented there to wake it up and optimistically marked it for 2.1. I doubt it will get in, though, since it's pretty late in the game. If there were a patch however (hint, hint), it would have a MUCH better chance. |
@thedarkone: Is this something I should open an issue about? I think it would be a very nice feature. |
Opened #30 (won't be able to work on this right now though..). |
Sorta related question, why does this library use autoload which is known not to be thread-safe? (Or so I understand)
H/T for the quote from my blog post and the ruby rogues discussion around it :) |
@thedarkone So, the internet is wrong inasmuch as And you'd advise using it in all libraries that support 1.9.3 or higher? Do we know specifics? Is there a source for this? I think a lot of people don't know. Update: I did find a few sources to support, but specifics still not clear, and it's kind of important, no?
|
* This is based on the comments in headius/thread_safe#15 (comment) on how to handle this exact issue with memoization.
* This is based on the comments in headius/thread_safe#15 (comment) on how to handle this exact issue with memoization.
I think this issue has been discussed enough. If there are things we need to fix please file separate issues. Summary:
|
@headius Thanks for following up. I think there's still a great need to better understand when/how to use autoload when building libraries. |
It is a misconception that:
(https://github.com/headius/thread_safe/blob/master/lib/thread_safe.rb#L32)
It looks like that code is saying if it's MRI, thread_safe gem provides nothing, just set ThreadSafe::Array to ordinary Array and ThreadSafe::Hash to ordinary hash.
But this is a misconception. While it's true that MRI can't run code on more than one core simultaneously, MRI can still context switch in the middle of any operation, switching one thread out for another. This means you still need to use synchronization, exactly the same way you do in a ruby without the GIL. (the ruby stdlib doesn't provide mutex and monitor and other synchronization primitives for no reason!) See: http://www.rubyinside.com/does-the-gil-make-your-ruby-code-thread-safe-6051.html
I could really use a simple threadsafe Array and Hash in MRI too. But I think I'm not getting it here?
I wonder if the rbx variation would just work in MRI too, it looks like maybe it only uses stdlib available on mri too?
The text was updated successfully, but these errors were encountered: