-
Notifications
You must be signed in to change notification settings - Fork 419
Writing cache for long running computation #1010
-
Hi, first of all thanks for the great gem! Now I know this is hinted at in the docs but I was looking for examples, the problem is - I'm writing a cache for long running computation based on Concurrent::Map and I want to make sure only single thread runs the computation in case the value is absent (with other threads waiting for result from this single thread), is it possible using just the Concurrent::Map (as far as I can tell it isn't), and if not what primitives are the best/most robust to choose?
Beta Was this translation helpful? Give feedback.
All reactions
It took me a long while to become familiar with the library 🤗
Concurrent Ruby borrows quite a lot from Java's concurrency primitives. You might start there: https://docs.oracle.com/javase/tutorial/essential/concurrency/atomic.html
Replies: 2 comments 2 replies
-
Concurrent::Map#compute_if_absent's documentation describes it as "atomic", meaning it should only execute on one thread. If you're running CRuby, you can see that the computation part is synchronized:
Beta Was this translation helpful? Give feedback.
All reactions
-
Ok, seems like I misunderstood the docs somewhat, is there any place I can read up on the definition of "atomic"?
Beta Was this translation helpful? Give feedback.
All reactions
-
It took me a long while to become familiar with the library 🤗
Concurrent Ruby borrows quite a lot from Java's concurrency primitives. You might start there: https://docs.oracle.com/javase/tutorial/essential/concurrency/atomic.html
Beta Was this translation helpful? Give feedback.
All reactions
-
❤️ 1
-
Ok, this works for me then for now, now gotta figure out why all these concurrent calls in my code...
Beta Was this translation helpful? Give feedback.