Skip to content

Commit

Permalink
Merge pull request #2198 from ruby/ractore_store_if_absent
Browse files Browse the repository at this point in the history
Add `Ractor.store_if_absent`
  • Loading branch information
soutaro authored Dec 24, 2024
2 parents aff0d7d + 121e43c commit ad2e348
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions core/ractor.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,23 @@ class Ractor
#
def self.shareable?: (untyped obj) -> bool

# <!--
# rdoc-file=ractor.rb
# - Ractor.store_if_absent(key){ init_block }
# -->
# If the correponding value is not set, yield a value with init_block and store
# the value in thread-safe manner. This method returns corresponding stored
# value.
#
# (1..10).map{
# Thread.new(it){|i|
# Ractor.store_if_absent(:s){ f(); i }
# #=> return stored value of key :s
# }
# }.map(&:value).uniq.size #=> 1 and f() is called only once
#
def self.store_if_absent: (Symbol) { () -> untyped } -> untyped

# <!--
# rdoc-file=ractor.rb
# - Ractor.yield(msg, move: false) -> nil
Expand Down

0 comments on commit ad2e348

Please sign in to comment.