[前][次][番号順一覧][スレッド一覧]

ruby-changes:69340

From: Koichi <ko1@a...>
Date: Sat, 23 Oct 2021 01:33:11 +0900 (JST)
Subject: [ruby-changes:69340] df9fac5ccd (master): update doc/ractor.md about ivars

https://git.ruby-lang.org/ruby.git/commit/?id=df9fac5ccd

From df9fac5ccd905d57251ffa89f28959c1b2a294eb Mon Sep 17 00:00:00 2001
From: Koichi Sasada <ko1@a...>
Date: Fri, 22 Oct 2021 18:22:00 +0900
Subject: update doc/ractor.md about ivars

---
 doc/ractor.md | 39 ++++++++++++++++++++++++++++++---------
 1 file changed, 30 insertions(+), 9 deletions(-)

diff --git a/doc/ractor.md b/doc/ractor.md
index dd1d44e098..5264a76861 100644
--- a/doc/ractor.md
+++ b/doc/ractor.md
@@ -565,27 +565,48 @@ Note that some special global variables are ractor-local, like `$stdin`, `$stdou https://github.com/ruby/ruby/blob/trunk/doc/ractor.md#L565
 
 ### Instance variables of shareable objects
 
-Only the main Ractor can access instance variables of shareable objects.
+Instance variables of classes/modules can be get from non-main Ractors if the referring values are shareable objects.
 
 ```ruby
 class C
-  @iv = 'str'
+  @iv = 1
 end
 
-r = Ractor.new do
+p Ractor.new do
   class C
-    p @iv
+     @iv
   end
-end
+end.take #=> 1
+```
 
+Otherwise, only the main Ractor can access instance variables of shareable objects.
 
-begin
-  r.take
-rescue => e
-  e.class #=> Ractor::IsolationError
+```ruby
+class C
+  @iv = [] # unshareable object
 end
+
+Ractor.new do
+  class C
+    begin
+      p @iv
+    rescue Ractor::IsolationError
+      p $!.message
+      #=> "can not get unshareable values from instance variables of classes/modules from non-main Ractors"
+    end
+
+    begin
+      @iv = 42
+    rescue Ractor::IsolationError
+      p $!.message
+      #=> "can not set instance variables of classes/modules by non-main Ractors"
+    end
+  end
+end.take
 ```
 
+
+
 ```ruby
 shared = Ractor.new{}
 shared.instance_variable_set(:@iv, 'str')
-- 
cgit v1.2.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]