Skip to content

Commit ad2e348

Browse files
authored
Merge pull request #2198 from ruby/ractore_store_if_absent
Add `Ractor.store_if_absent`
2 parents aff0d7d + 121e43c commit ad2e348

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

core/ractor.rbs

+17
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,23 @@ class Ractor
588588
#
589589
def self.shareable?: (untyped obj) -> bool
590590

591+
# <!--
592+
# rdoc-file=ractor.rb
593+
# - Ractor.store_if_absent(key){ init_block }
594+
# -->
595+
# If the correponding value is not set, yield a value with init_block and store
596+
# the value in thread-safe manner. This method returns corresponding stored
597+
# value.
598+
#
599+
# (1..10).map{
600+
# Thread.new(it){|i|
601+
# Ractor.store_if_absent(:s){ f(); i }
602+
# #=> return stored value of key :s
603+
# }
604+
# }.map(&:value).uniq.size #=> 1 and f() is called only once
605+
#
606+
def self.store_if_absent: (Symbol) { () -> untyped } -> untyped
607+
591608
# <!--
592609
# rdoc-file=ractor.rb
593610
# - Ractor.yield(msg, move: false) -> nil

0 commit comments

Comments
 (0)