Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Writing cache for long running computation #1010

Answered by bensheldon
dolzenko asked this question in Q&A
Discussion options

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?

You must be logged in to vote

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

Comment options

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:

https://github.com/ruby-concurrency/concurrent-ruby/blob/9f40827be9a8a192a6993a8d157bd3ed0662ada0/lib/concurrent-ruby/concurrent/collection/map/mri_map_backend.rb#L21C1-L27

You must be logged in to vote
0 replies
Comment options

Ok, seems like I misunderstood the docs somewhat, is there any place I can read up on the definition of "atomic"?

You must be logged in to vote
2 replies
Comment options

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

Answer selected by dolzenko
Comment options

Ok, this works for me then for now, now gotta figure out why all these concurrent calls in my code...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /