1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

update doc/ractor.md about ivars

This commit is contained in:
Koichi Sasada 2021-10-22 18:22:00 +09:00
parent a7776077be
commit df9fac5ccd
Notes: git 2021-10-23 01:33:20 +09:00

View file

@ -565,27 +565,48 @@ Note that some special global variables are ractor-local, like `$stdin`, `$stdou
### Instance variables of shareable objects ### 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 ```ruby
class C class C
@iv = 'str' @iv = 1
end end
r = Ractor.new do p Ractor.new do
class C class C
p @iv @iv
end end
end end.take #=> 1
begin
r.take
rescue => e
e.class #=> Ractor::IsolationError
end
``` ```
Otherwise, only the main Ractor can access instance variables of shareable objects.
```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 ```ruby
shared = Ractor.new{} shared = Ractor.new{}
shared.instance_variable_set(:@iv, 'str') shared.instance_variable_set(:@iv, 'str')