From 121e43c2b7500d40f1ae1889e5cdbe9ec3a9baa4 Mon Sep 17 00:00:00 2001 From: Soutaro Matsumoto Date: Tue, 24 Dec 2024 15:46:10 +0900 Subject: [PATCH] Add `Ractor.store_if_absent` --- core/ractor.rbs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/core/ractor.rbs b/core/ractor.rbs index 48b0dfd02..731e835a2 100644 --- a/core/ractor.rbs +++ b/core/ractor.rbs @@ -588,6 +588,23 @@ class Ractor # def self.shareable?: (untyped obj) -> bool + # + # 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 + #