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M}$r0$9e(B

    f.pass

  • e$B%U%!%$%P$X!"CMIU$-$G=hM}$r0$9e(B

    f.pass(value)

  • e$BJ#?t$N%U%!%$%P$G=hM}$r0\F0$7$"$&e(B

    f2 = nil
    f1 = Fiber.new{f2.pass; f2.pass}
    f2 = Fiber.new{f1.pass; f1.pass}
    f1.pass

  • e$B8=:_F0$$$F$$$ke(B Fiber e$B$rCN$ke(B

    Fiber.current # Thread.current e$B$HF1$8H/A[$G$9!#e(B

  • e$B:G8e$K=hM}$r0$7$?e(B Fiber e$B$rCN$ke(B

    Fiber.prev e$B$b$7$/$Oe(B Fiber#prev

    e$BMxMQNce(B

    f0 = Fiber.current
    f1 = Fiber.new{
    p(Fiber.prev == f0) #=> true
    }
    f1.pass

  • e$B0EL[$K@8@.$5$l$ke(B Fiber e$B$rCN$ke(B

e$B!!%9%l%C%I$,@8@.$5$l$k$H!“0EL[$N$&$A$K$R$H$De(B Fiber
e$B$,@8@.$5$l$k$3$H$K$Je(B
e$B$j$^$9!#$=$l$r!”$3$3$G$OJX59>ee(B root Fiber
e$B$H8@$&$3$H$K$7$^$9!#<B$O$^$@<Be(B
e$BAu$7$F$$$^$;$s$,!“e(BFiber.root
e$B$G$H$l$k$H$$$$$+$J!”$H;W$C$F$$$^$9!#$,!"e(B
e$B<h$C$F2?$9$k$N$+$O$h$/$o$+$j$^$;$s!#e(B

e$B!!$A$J$_$K!“e(Broot Fiber
e$B$,=*N;$7$?;~E@$G4XO”$E$1$i$l$F$$$k%9%l%C%I$O=*N;e(B
e$B$9$k$3$H$K$J$j$^$9!#e(B

  • Fiber e$B=*N;;~$N=hM}$NN.$le(B

e$B!!$$$m$$$m9M$($F!"<!$N$h$&$K$9$k$h$&$K$7$^$7$?!#e(B

(a) e$B$^$:!"e(BFiber.prev e$B$Ke(B pass
(b) e$B$?$@$7!"e(BFiber.prev
e$B$,$9$G$K=*N;$7$F$$$k>l9g!"e(BFiber.root e$B$Xe(B pass

e$B!!e(BFiber e$B$KEO$9%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%j<BAu$K$9$k!”$He(B
e$B$$$&$N$b<j$+$H;W$$$^$9!#e(B

e$B!!$G!"$$$m$$$m%a%=%C%IL>$r9M$($F$_$^$7$?!#e(B

class Fiber
alias start pass # e$B=i2s0J30$G$b;H$($F$$$$$b$N$+e(B (*)
alias restart pass # e$B=i2s$K;H$($F$$$$$b$N$+e(B
alias resume pass # suspend e$B$7$F$J$$$N$K;H$($F$$$$$b$N$+e(B
alias kick pass
alias call pass # call e$B$@$H!"BPEy$N4X78$8$c$J$$$C$]$$e(B
alias goto pass # fib.goto e$B$OJQ$@e(B
alias yield pass # e$B5!G=J6$i$o$7$/$J$$!)e(B
alias transit pass

def suspend *args # e$B8F$S=P$785$XLa$k!#e(Bsemi-coroutine /
e$B3N$+$KJXMxe(B
Fiber.prev.pass *args
end
end

(*)
e$B$7$+$7!"=i2s$+$I$&$+$r6hJL$9$k$?$a$K%U%i%0$r@_$1$k$N$bL5BL$C$]$$!#e(B

e$B!!$b$&!">e5-$NL>A0$rA4It%5%]!<%H$9$k!"$H$+$G$b$$$$$N$+$b$7$l$^$;$s!#$&!<e(B
e$B$s!"$d$C$Q$jBLL$+$J!#$h$/$o$+$j$^$;$s!#$I$&$7$^$7$g$&!#e(B

(4) e$B$b$C$H$[$+$Ne(B API

e$B!!B>$K$bI,MW$Je(B API e$B$C$FMW$k$G$7$g$&$+!#e(B

  • e$BNce(B

e$B!!$h$/$"$j$=$&$J!"e(BProducer/Consumer e$B$NNc$G$9!J>e5-$Ne(B yield
e$B$Ne(B alias e$B$re(B
e$B;H$C$F$$$^$9!K!#e(B

producer/consumer sample

root = Fiber.current
consumer = nil

producer = Fiber.new{
10000.times{|e|
consumer.pass e
}
consumer.pass nil
}

consumer = Fiber.new{
while v = producer.yield
p v
end
}

producer.pass

e$B!!e(BFiber e$B$Ge(B Generator e$B$r:n$C$?Nc$G$9!J>e5-$Ne(B
suspend/resume e$B$r;H$C$F$$$^e(B
e$B$9!K!#e(BThreade$B!J$de(B
callcce$B!K$r;H$&$h$j!"$:$$$V$s$9$C$-$j$7$F$$$^$9!#e(B

Generator sample

class Generator
def initialize enum = nil, &block
@finished = false
@index = 0
@enum = enum
@block = block

@fib = if block_given?
  Fiber.new{
    yield self
    @finished = true
  }
else
  Fiber.new{
    enum.each{|e|
      @e = e
      @fib.suspend
    }
    @finished = true
  }
end
@fib.start

end

def next?
!@finished
end

def next
raise “No element remained” if @finished
ret = @e
@index += 1
@fib.resume
ret
end

def end?
!self.next
end

def yield value
@e = value
@fib.suspend
end

def current
@e
end

def index
@index
end

alias pos index

def rewind
initialize(@enum, &@block) if @index.nonzero?
self
end
end

def show g
while g.next?
p g.next
end
end

show Generator.new([:a, :b, :c])
show Generator.new(1…3)
show Generator.new{|g|
g.yield 10
g.yield 20
g.yield 30
}


#2

e$BA0ED$G$9!#e(B

On 05/28/2007 05:01 AM, SASADA Koichi wrote:

e$B!!e(BContinuation e$B$N$D$$$G$Ke(B Fiber e$B$rF~$l$^$7$?!#6bMKF|$K$O=PMh$F$?$s$G$9e(B
e$B$,!"$b$m$b$m$"$C$F%3%_%C%H$,CY$/$J$C$F$7$^$$$^$7$?!#e(B

e$B$$!#e(B

(1) e$B$=$b$=$b!"e(BRuby e$B$K$3$s$J5!G=$r2C$($F$$$$$G$9$+!)e(B

e$B!!$H$j$"$($:!"e(BGenerator e$B$OBgJQ=q$-$d$9$+$C$?$G$9!#e(B

e$B$$$$$H;W$&$N$G$9$,!"e(BRubye$B%l%Y%k$G7QB3$r;H$C$F=q$+$J$$$N$O%Q%U%)!<%^%s%9$Ne(B
e$B$?$a$G$9$+e(B?

(2) Fiber e$B$H$$$&L>A0$OE,Ev$G$7$g$&$+!)e(B

e$B!!e(BCoroutine e$B$H$$$&L>A0$N$[$&$,$$$$$N$+$J$!!"$H$$$&5$$b$7$F$$$^$9!#e(BFiber
e$B$@$H!“e(BThread e$B$K0z$-$:$i$l$F8m2r$,@8$8$k2DG=@-$,$”$k$+$b$7$l$^$;$s!#e(B
e$B$A$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$jBX$($Oe(B VM
e$B%9%?%C%/$N%3%T!<e(B
e$B$,I,MW$G$9$,!“e(BFiber e$B$N>l9g!”$=$3$Oe(B VM
e$B%9%?%C%/%]%$%s%H$N@Z$jBXe(B
e$B$($G:Q$`$+$i$G$9!#%^%7%s%9%?%C%/$N%3%T!<$O!"$I$A$i$bI,MW$J$N$Ge(B
e$B$9$,!#e(B

passe$B$h$je(Brune$B$NJ}$,=hM}$,0$k@h$,$o$+$j$d$9$$$h$&$K;W$$$^$9!#e(B
e$B!!$&!<$s!“e(Brun
e$B$O;W$$$D$-$^$;$s$G$7$?!#$J$s$H$J$/!”:G=i$N0l2s$@e(B
e$B$1$N%a%=%C%IL>$K8+$($k$N$O5$$N$;$$$G$7$g$&$+!#e(B


#4

e$B$^$D$b$He(B e$B$f$-$R$m$G$9e(B

In message “Re: [ruby-dev:30844] Re: Supporting Fiber”
on Wed, 30 May 2007 18:06:07 +0900, “NAKAMURA, Hiroshi”
removed_email_address@domain.invalid writes:

|>>> (3) API e$BL>$OE,Ev$G$7$g$&$+!)e(B
|
|e$B3N$+$K!"e(BFiber#rune$B$O%T%s$HMh$^$;$s$G$7$?!#!V5/$3$9!W7O$,$$$$$H;W$$$^$7$?e(B
|e$B$,!"e(BThreade$B$H$NN`;w$OHr$1$?$[$&$,$$$$$b$N$J$s$G$7$g$&$+!#8=>u$G$I$l$+$H8@e(B
|e$B$($Pe(Bpasse$B$+$J!#!#!#e(B

e$B6(D47?%9%l%C%I$G$OEAE}E*$Ke(Byielde$B$H$$$&C18l$,;H$o$l$F$$$^$9$,!“e(B
e$BM=Ls8l$H=E$J$k$N$G$h$/$J$$$+$J$”!#e(B


#5

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

SASADA Koichi wrote:

(1) e$B$=$b$=$b!"e(BRuby e$B$K$3$s$J5!G=$r2C$($F$$$$$G$9$+!)e(B

e$BLLGr$=$&$J$N$G$h$$$H;W$$$^$9!#$H!"$[$C$H$+$l$G$b<d$7$$$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(B

e$B6(D47?%9%l%C%I$G$OEAE}E*$Ke(Byielde$B$H$$$&C18l$,;H$o$l$F$$$^$9$,!“e(B
e$BM=Ls8l$H=E$J$k$N$G$h$/$J$$$+$J$”!#e(B

e$B!!$3$l$O!"e(BFiber#pass e$B$r!“A4Ite(B Fiber#yield
e$B$KJQ$($k$Y$-!”$H$$$&e(B
e$B$3$H$G$7$g$&$+!#e(B

root = Fiber.current
consumer = nil

producer = Fiber.new{
10000.times{|e|
consumer.yield e # (a)
}
consumer.yield nil
}

consumer = Fiber.new{
while v = producer.yield # (b)
p v
end
}

producer.yield # ©

e$B!!e(B(a), (b) e$B$Oe(B yield e$B$GNI$$$h$&$J5$$,$9$k$s$G$9$,!"e(B©
e$B$be(B yield
e$B$G$$$$$G$7$g$&$+!#$$$$$h$&$J5$$b$7$^$9$,!#e(B

e$B!!$*$C$7$c$kDL$j!“M=Ls8l$H$Ne(B yield
e$B$H:.Mp$9$k!”$H$$$&$[$&$,32e(B
e$B$,Bg$-$$$+$b$7$l$^$;$s!#e(B

e$B!!e(BLua e$B$Ne(B API e$B$O!"e(Bcoroutine.resume, coroutine.yield
e$B$H$J$k$h$&e(B
e$B$G$9!#e(B

e$B!!:#$Ne(B API e$B$rMxMQ$7$F5-=R$9$k$H!"e(B

class Fiber
def self.resume fiber, *args
fiber.pass *args
end

def self.yield *args
Fiber.prev.pass *args
end
end

e$B$H$$$&$h$&$G$9!J%/%i%9%a%=%C%IAjEv$G!"e(Byield e$B$OD>A0$Ke(B resume
e$B$7e(B
e$B$?@h$XLa$k!K!#e(Bresume
e$B$,@Z$jBX$(@h%U%!%$%P$r0z?t$K<h$k$N$,6=L#e(B
e$B?<$$$G$9$M!#e(B


#7

e$B%l%*$G$9!#e(B

On 5/28/07, SASADA Koichi removed_email_address@domain.invalid wrote:

alias goto pass # fib.goto e$B$OJQ$@e(B
alias yield pass # e$B5!G=J6$i$o$7$/$J$$!)e(B
alias transit pass

Fiber#abdicate # “Fiber A abdicates control to Fiber B”
Fiber#bequeath # “Fiber A bequeaths control to Fiber B”
Fiber#bestow # “Fiber A bestows control to Fiber B”
Fiber#cede # “Fiber A cedes control to Fiber B”
e$B$O$$$+$,$G$7$g$&$+!)e(B
e$B$$$:$l$b!Ve(Byielde$B!W$H>/$7;w$F$$$F!"$^$?$Oe(Bswitche$B$H0c$C$F$$$F!“e(B
_toe$B$rIU$12C$($J$/$F$b!Ve(BAe$B$+$ie(BBe$B$X>y$k!W$N%K%e%”%s%9$,$"$j$^$9!#e(B

e$B8D?ME*$Ke(Bbestowe$B$,$$$$$H;W$&!&!&!&e(B

e$B>/$7$$Fq$7$$8lWC$G$"$k$3$H$OFqE@$G$9!#e(B


#8

e$BA0ED$G$9!#e(B

On 05/30/2007 03:33 AM, SASADA Koichi wrote:

e$B$9$,!#e(B
e$BN;2r$G$9!#e(B
e$B$7$+$7!“e(BFibere$B$,F~$C$F!”$7$+$b<BAu$K7QB3$,;H$o$l$J$$$H$J$k$H!"e(B
e$B$^$9$^$97QB3$NB8:_0U5A$,$J$/$J$j$=$&$G$9$M!#e(B

e$B$$$d!"7QB3$r$J$/$;$H8@$C$F$k$o$1$G$O$J$$$G$9$h!D!#e(B

e$B$H$O$$$(e(BFiber.switch_to(fib)e$B$O$+$C$3$o$k$$$N$G!“e(BFiber#rune$B$+$J$”!#e(B
e$B$I$C$A$+$H$$$&$H!"%$%a!<%8E*$K6a$$$N$Oe(Bpasse$B$h$je(Brune$B$8$c$J$$$G$9$+$M!#e(B
Thread#rune$B$H$OI,$:=hM}$,0$k$+$I$&$+$H$$$&0c$$$O$"$k$o$1$G$9$1$I!"e(B
passe$B$h$je(Brune$B$NJ}$,=hM}$,0$k@h$,$o$+$j$d$9$$$h$&$K;W$$$^$9!#e(B

e$B!!$&!<$s!“e(Brun e$B$O;W$$$D$-$^$;$s$G$7$?!#$J$s$H$J$/!”:G=i$N0l2s$@e(B
e$B$1$N%a%=%C%IL>$K8+$($k$N$O5$$N$;$$$G$7$g$&$+!#e(B

e$B$=$l$r$$$&$He(BThread#rune$B$bJQ$H$$$&$3$H$K$J$j$^$9$h$M!#e(B

e$BJQ$J$N$+$b!D!#e(B

Javae$B$@$He(Brune$B$Oe(Bresumee$B$G$7$?$C$1!#e(B

On 05/30/2007 06:06 PM, NAKAMURA, Hiroshi wrote:

(3) API e$BL>$OE,Ev$G$7$g$&$+!)e(B

e$B3N$+$K!"e(BFiber#rune$B$O%T%s$HMh$^$;$s$G$7$?!#!V5/$3$9!W7O$,$$$$$H;W$$$^$7$?e(B
e$B$,!"e(BThreade$B$H$NN`;w$OHr$1$?$[$&$,$$$$$b$N$J$s$G$7$g$&$+!#8=>u$G$I$l$+$H8@e(B
e$B$($Pe(Bpasse$B$+$J!#!#!#e(B

e$B!V5/$3$9!W$@$H$d$O$je(BThreade$B$+$i<h$C$Fe(BFiber#wakeupe$B$G$9$+$M!#e(B


#9

e$B$J$+$@$G$9!#e(B

At Fri, 1 Jun 2007 13:59:22 +0900,
Leonard C. wrote in [ruby-dev:30866]:

alias goto pass # fib.goto e$B$OJQ$@e(B
alias yield pass # e$B5!G=J6$i$o$7$/$J$$!)e(B
alias transit pass

Fiber#abdicate # “Fiber A abdicates control to Fiber B”
Fiber#bequeath # “Fiber A bequeaths control to Fiber B”
Fiber#bestow # “Fiber A bestows control to Fiber B”
Fiber#cede # “Fiber A cedes control to Fiber B”
e$B$O$$$+$,$G$7$g$&$+!)e(B
e$B$$$:$l$b!Ve(Byielde$B!W$H>/$7;w$F$$$F!"$^$?$Oe(Bswitche$B$H0c$C$F$$$F!“e(B
_toe$B$rIU$12C$($J$/$F$b!Ve(BAe$B$+$ie(BBe$B$X>y$k!W$N%K%e%”%s%9$,$"$j$^$9!#e(B

e$B$=$l$OA4Ite(BAe$B$,<g8l$Ge(BBe$B$,L\E*8l$G$9$h$M!#:#$Ne(BFiber#passe$B$G$Oe(B
Fiber.currente$B$+$i%l%7!<%P$K@)8f$,EO$k$N$G!"J}8~$,5U$G$O$J$$$+$He(B
e$B;W$$$^$9!#e(BFibere$B$N%/%i%9%a%=%C%I$J$ie(B

Fiber.bestow(fiberB)

e$B$G$b$$$$$N$G$7$g$&$,!#e(B

e$B$J$K$+!"!VG$$5$l$k!W$H$$$&$h$&$JC18l$O$J$$$G$7$g$&$+!#e(B

Fiber#have_control


#10

e$B!!$5$5$@$G$9!#e(B

Leonard C. e$B$5$s$O=q$-$^$7$?e(B:

e$BNc!Ke(B
next_fiber.take_over

e$B!!$$$m$$$m$J0F!"$"$j$,$H$&$4$6$$$^$9!#$?$@!“CM$b0l=o$KEO$9$H$Je(B
e$B$k$H!”$=$l$b$A$g$C$H0c$&$+$J$!$H$$$&5$$,$7$F$$$^$9!#$H$j$"$(e(B
e$B$:!"e(BFiber#yield e$B$K$7$F$$$^$9!#e(B

e$B!!e(BRubinius e$B$N?M$?$A$Oe(B Task.current=
e$B$H$$$&%a%=%C%I$G@Z$jBX$($ke(B
e$B$i$7$$!#;B?7!JCM$OEO$;$J$$$s$@$1$I!K!#e(B


#11

e$B1sF#$G$9!#e(B

Fiber e$B$N5sF0$K$D$$$F<ALd$G$9!#e(B

e$B8=>u$G!"0J2<$N%3!<%I$@$He(B 1 e$B$@$1=PNO$5$l$^$9$,!"e(B

f = Fiber.new do
p 1
Fiber.prev.yield
p 2
end
f.yield

e$B0J2<$N%3!<%I$@$He(B 1 e$B$He(B 2 e$B$,=PNO$5$l$^$9!#e(B

f = Fiber.new do
p 1
Fiber.prev.yield
p 2
end
Fiber.new do
f.yield
end.yield

e$B$3$N0c$$$O0U?^$5$l$?$b$N$G$7$g$&$+!#e(B
e$B8D?ME*$K$O!"$I$A$i$be(B 1 e$B$@$1=PNO$5$l$k$N$,D>46$K$"$$$^$9!#e(B


#12

e$B!!$5$5$@$G$9!#e(B

Yusuke ENDOH e$B$5$s$O=q$-$^$7$?e(B:

e$B8D?ME*$K$O!"$I$A$i$be(B 1 e$B$@$1=PNO$5$l$k$N$,D>46$K$"$$$^$9!#e(B
e$B!!$3$l$O!"!Ve(BFiber
e$B$,=N;$7$?>l9g!"$I$&$9$k$+!W$H$$$&LdBj$K$J$je(B
e$B$^$9!#$3$3$O!"$$$/$D$+A
Br;h$,$"$k$s$G$9$,!"8=:_$G$Oe(B
[ruby-dev:30827] e$B$K=q$$$?$H$*$j!"e(B

p Fiber.new{
:ok
}.pass #=> :ok

e$B$H$J$j$^$9!#e(B

e$B$H$J$C$F$$$^$9!#$I$&$9$k$H$h$$$H;W$$$^$9$+!)e(B


#13

e$B%l%*$G$9e(B

On 6/1/07, Nobuyoshi N. removed_email_address@domain.invalid wrote:

e$B$=$l$OA4Ite(BAe$B$,<g8l$Ge(BBe$B$,L\E*8l$G$9$h$M!#:#$Ne(BFiber#passe$B$G$Oe(B
Fiber.currente$B$+$i%l%7!<%P$K@)8f$,EO$k$N$G!"J}8~$,5U$G$O$J$$$+$He(B
e$B;W$$$^$9!#e(BFibere$B$N%/%i%9%a%=%C%I$J$ie(B

Fiber.bestow(fiberB)

e$B$G$b$$$$$N$G$7$g$&$,!#e(B

e$B$J$K$+!"!VG$$5$l$k!W$H$$$&$h$&$JC18l$O$J$$$G$7$g$&$+!#e(B

e$B$9$_$^$;$s!"4*0c$$$7$^$7$?!#3N$+$K!"5U$N>l9g!J%l%7!<%P$+$i0z?te(B
e$B$K@)8f$,$o$?$k>l9g!K$r9M$($F$$$^$7$?!#e(B

e$B!VG$$5$l$k!W$J$i$P!'e(B
Fiber#take_over # “Fiber A takes over”
Fiber#usurp # “Fiber A usurps control”
Fiber#accede # “Fiber A accedes control from B”
Fiber#succeed # “Fiber A succeeds B” e$B$A$g$C$H7Q>5$N0UL#9g$$$,$"$ke(B

e$B>e5-$N$&$A!"e(BFiber#take_overe$B$N8l46$,7k9=$$$$$H;W$$$^$9!#e(B
e$BNc!Ke(B
next_fiber.take_over


#14

e$B1sF#$G$9!#e(B

e$B!!$3$l$O!"!Ve(BFiber e$B$,=N;$7$?>l9g!"$I$&$9$k$+!W$H$$$&LdBj$K$J$je(B
e$B$^$9!#$3$3$O!"$$$/$D$+A
Br;h$,$"$k$s$G$9$,!"8=:_$G$Oe(B
[ruby-dev:30827] e$B$K=q$$$?$H$*$j!"e(B

e$B$2!"$^$?FI$_Mn$H$7$F$^$7$?!#<:Ni$7$^$7$?!#e(B

e$B$H$J$C$F$$$^$9!#$I$&$9$k$H$h$$$H;W$$$^$9$+!)e(B

e$B0lHLE*$J0UL#$K$"$o$;$k$N$,0lHV$$$$$H;W$$$^$9!#e(B

e$BITJY6/$J$,$i;d$Oe(B fiber e$B$de(B coroutine
e$B4XO"$NO@J8$dB>8@8l$N;EMM$re(B
e$BFI$s$@$3$H$,$J$$$N$G!"$=$NEz$($O$o$+$j$^$;$s!#$9$_$^$;$s!#e(B

e$B;d$,e(B [ruby-dev:30924] e$B$N5sF0$K0cOB46$r3P$($?M}M3$Oe(B Fiber
e$B$N0UL#$re(B
callcc
e$B$NC1=c$J%i%C%Q$H$7$FM}2r$7$F$$$F!"$=$l$HF1$85sF0$r4|BT$7$?$+$i$G$9!#e(B
e$B$D$^$j!"e(B[ruby-dev:30924] e$B$O0J2<$H$[$\Ey2A$+$H!#e(B

f = proc do |prev|
p 1
prev.call
p 2
end

f2 = proc do |prev|
callcc {|current| f.call(current) }
end

callcc {|current| f2.call(current) }

e$BG>FbJQ495,B’e(B

Fiber.new ==> proc {|prev| … }

Fiber#yield ==> callcc {|current| self.call(current) }

e$B$7$+$7$3$NJQ495,B’$@$He(B Fiber
e$B$rF3F~$9$k0UL#$,$[$H$s$I$J$5$=$&$J$N$Ge(B
e$B$3$A$i$NJ}$,0lHLE*$K:NMQ$5$l$F$$$k$H$+!"JXMx$JNc$,B?$=$&$@$H$$$&$3$H$J$i!"e(B
e$B;d$N0cOB46$OL5;k$7$F$/$@$5$$!#e(B

e$B$"!"$"$H!“8F$S=P$7$re(B Fiber.new { … }.yield
e$B$G$/$/$k$@$1$G5sF0$,JQ$o$ke(B
e$B4X?t$,Dj5A$G$-$F$7$^$&$3$H$KDq93$,$”$C$?$H$$$&$N$b$"$j$^$9!#e(B
e$B$=$&$$$&9uKb=Q$Oe(B callcc e$B$@$1$G$$$$!"$_$?$$$J!#e(B

def foo
Fiber.new do
p 1
Fiber.prev.yield
p 2
end.yield
end

Fiber.new do

# e$B2?$+$$$m$$$m$d$ke(B
foo
# e$B2?$+$$$m$$$m$d$ke(B
# Fiber.new e$B$G$/$/$C$F$$$k$HK:$l$?:"$Ke(B p 2 e$B$,<B9T$5$l$ke(B

end.yield


#15

e$B!!$5$5$@$G$9!#e(B

Yusuke ENDOH e$B$5$s$O=q$-$^$7$?e(B:

e$B0lHLE*$J0UL#$K$"$o$;$k$N$,0lHV$$$$$H;W$$$^$9!#e(B

e$BITJY6/$J$,$i;d$Oe(B fiber e$B$de(B coroutine e$B4XO"$NO@J8$dB>8@8l$N;EMM$re(B
e$BFI$s$@$3$H$,$J$$$N$G!"$=$NEz$($O$o$+$j$^$;$s!#$9$_$^$;$s!#e(B

f2 = proc do |prev|
e$B$7$+$7$3$NJQ495,B’$@$He(B Fiber e$B$rF3F~$9$k0UL#$,$[$H$s$I$J$5$=$&$J$N$Ge(B
e$B$3$A$i$NJ}$,0lHLE*$K:NMQ$5$l$F$$$k$H$+!"JXMx$JNc$,B?$=$&$@$H$$$&$3$H$J$i!"e(B
e$B;d$N0cOB46$OL5;k$7$F$/$@$5$$!#e(B

e$B!!$[$+$N8@8l$NNc$O<B$O;n$7$F$$$J$$$s$G$9$,!“e(Bwin32api e$B$Ne(B
Fiber
e$B$Oe(B SEGV e$B$7$^$7$?!#$3$l$O$”$s$^$j$@!#e(B

e$B!!3NG’$7$F$$$k$N$Oe(B lua e$B$He(B modula-2
e$B$J$s$G$9$,!"$=$l$>$le(B Fiber
e$B!Je(BCoroutinee$B!K$N=*N;;~$K$O$I$&$J$k$N$+65$($F$/$l$k?M$O$$$J$$$Ge(B
e$B$9$+$M!#e(Blua e$B$J$i;n$;$k$+$J!#e(B

e$B!!$H$j$"$($:!"$$$/$D$+%"%W%j%1!<%7%g%s$r:n$C$F$_$F$+$i9M$($?$[e(B
e$B$&$,$$$$$h$&$J5$$,$7$F$$$^$9!#e(B

end

Fiber.new do

e$B2?$+$$$m$$$m$d$ke(B

foo

e$B2?$+$$$m$$$m$d$ke(B

Fiber.new e$B$G$/$/$C$F$$$k$HK:$l$?:"$Ke(B p 2 e$B$,<B9T$5$l$ke(B

end.yield

e$B!!I=8=NO$N9b$$e(B coroutine
e$B$r<B8=$9$k$?$a$N;EAH$_$J$N$G!"!J$o$+e(B
e$B$i$J$$$H!KIT;W5D$J$3$H$,$"$k$N$O$=$&$$$&$b$N$+$J$!!"$H;W$$$^e(B
e$B$9!#$^$?!"$[$+$NNc$G$bF1$8$h$&$J8=>]$,5/$3$j$^$9!#$?$H$($P!“e(B
Thread#passe$B!Je(BThread#criticale$B!K$G$OF1$8$h$&$J$3$H$,5/$3$j$^$9e(B
e$B$,!”$3$l$O9uKb=Q$G$7$g$&$+!#e(B

e$B!!$?$@$7!“2a>j$J<+M3EY$O:.Mp$5$;$k$@$1$J5$$b$7$F$$$^$9!#$J$Ne(B
e$B$G!”$?$H$($Pe(B python e$B$N$h$&$J@)8BIU$-e(B coroutine
e$B$N$h$&$K$7$FDse(B
e$B6!$9$k$N$b0l0F$+$H;W$C$F$$$^$9!#e(B

e$B!!$A$J$_$K!"e(Bpython e$B$G$Oe(B generator
e$B$,=*N;$7$?$iNc30$rJV$7$^$9!#e(B
e$B$3$l$O$3$l$GG<F@$G$-$kOC$G$9!#e(B


#16

e$B1sF#$G$9!#e(B

e$BD9J8<:Ni$7$^$9!#e(BLua e$B$He(B Modula-2
e$B$r$A$g$C$H$@$1;n$7$F$_$?$N$GJs9p$7$^$9!#e(B
e$B$O$8$a$F$Ne(B Lua e$B$He(B Modula-2
e$B$J$N$G4V0c$$$,$"$C$?$i$4$a$s$J$5$$!#e(B
e$B8!>Z$7$?%3!<%I$OKvHx$K:$;$^$9!#e(B

Lua e$B$Ne(B coroutine e$B$Oe(B semi-coroutine e$B$G!“e(Bcoroutine
e$B4V$K?F;R4X78$,$”$j$^$7$?!#e(B
e$B<B9T$r0$9%3%^%s%I$,Fs<oN`e(B (resume e$B$He(B yield)
e$B$"$j$^$9!#e(B
resume e$B$GB>$Ne(B coroutine e$B$r6nF0$7!"$=$Ne(B coroutine e$B$,e(B
yield e$B$9$k$N$rBT$A$^$9!#e(B
resume e$B$O?F$+$i;R$r!"e(Byield e$B$O;R$+$i?F$r6nF0$9$k46$8$G$9e(B
(e$B?F$O;R$rA*$Y$^$9$,!“e(B
e$B;R$O?F$rA*$Y$^$;$se(B) e$B!#e(B
coroutine e$B$,=*N;$9$k$H!”$=$Ne(B coroutine e$B$,e(B yield
e$B$7$?$N$HF1$8F0:n$r$7$^$7$?!#e(B
e$B?F;R4X78$r2u$9$h$&$JA+0\e(B (e$B;R$+$i?F$re(B resume e$B$9$ke(B)
e$B$O%(%i!<$K$J$j$^$7$?!#e(B
e$B=*N;$7$?e(B coroutine e$B$re(B resume e$B$7$F$b%(%i!<$G$7$?!#e(B

Modula-2 e$B$Ne(B coroutine e$B$O=c?he(B (?) e$B$Je(B coroutine
e$B$G!"e(Bcoroutine e$BF1;N$OBPEy$G$9!#e(B
e$B=>$C$F<B9T$r0$9%3%^%s%I$O0l<oNe(B (TRANSFER) e$B$@$1$G$9!#e(B TRANSFER e$B$GB>$Ne(B coroutine e$B$r6nF0$7$^$9!#e(BTRANSFER e$B$7$?e(B coroutine e$B$OB>$Ne(B coroutine e$B$+$ie(B TRANSFER e$B$5$l$k$^$G@dBP$KF0$-$^$;$s!#e(B coroutine e$B$,=*N;$9$k$H%W%m%0%i%$4$H=*N;$7$^$7$?!#e(B

Lua e$B<0$He(B Modula-2 e$B<0$N$I$A$i$,$$$$$+$O0lD90lC;$@$H;W$$$^$9!#e(B

1.9 e$B$Ne(B Fiber e$B$Oe(B Lua e$B$He(B Modula-2 e$B$NCf4Ve(B
(e$B$d$de(B Modula-2 e$B4s$je(B) e$B$K$$$k46$8e(B
e$B$G$7$g$&$+!#e(Bcoroutine e$B=*N;;~$Ke(B yield e$B$9$k$N$Oe(B Lua
e$B$C$]$$$G$9$,!"e(B
e$B<B9T$r0$9$N$,e(B 1 e$B<oN`$@$1$N$Oe(B Modula-2 e$B$C$]$$$G$9!#e(B

Lua e$B$Oe(B resume e$B$He(B yield e$B$N6hJL$K$h$C$Fe(B coroutine
e$B$N?F;R4X78$,J]>Z$5$l$F$*$j!“e(B
coroutine e$B$N=N;;~$N5sF0e(B (e$B?F$KLa$ke(B)
e$B$,4X?t8F$S=P$7$C$]$/$F$o$+$j$d$9$$$G$9!#e(B
Modula-2
e$B$O?F$b;R$b$J$/<B9T$O$?$@0lK$G!"$=$l$,ES@Z$l$?$iA4It=
$o$j!”$H$$$&!“e(B
e$BMpK=$J5sF0$G$O$”$j$^$9$,!"$3$l$O$3$l$G6Z$,DL$C$F$$$k$H;W$$$^$9!#e(B
1.9 e$B$N8=>u$Ne(B Fiber e$B$O$3$N$"$?$j$,CfESH>C<$J$N$Ge(B prev
e$B$,JQ$J$H$3$m$re(B
e$B;X$7$F$$$k46$8$,$7$^$9!#e(B

e$B$"$H1=$G$O!"e(BSimula e$B$Oe(B Lua e$B<0$He(B Modula-2
e$B<0$NN>J}$N5!G=$rDs6!$7$F$$$k$i$7$$e(B
(e$B$7$+$7$=$N$;$$$GHs>o$KJ#;($J<BAu$K$J$C$F$$$k$i$7$$e(B) e$B$G$9!#e(B

e$BHs>o$K$o$+$j$K$/$$%a!<%k$G$9$_$^$;$s!#e(B
e$B0J2<!“8!>Z$7$?%3!<%I$G$9!#”($N?t;z$N=g$K<B9T$5$l$^$9!#e(B

[[test.lua e$B$3$3$+$ie(B]]
c1 = coroutine.create(function ()
print(“1”) – e$B"(e(B 3 e$B"(e(B
coroutine.yield() – e$B"(e(B 4 e$B"(e(B
print(“2”) – e$B"(e(B e$BE~C#$7$J$$e(B e$B"(e(B
end)
c2 = coroutine.create(function ()
coroutine.resume(c1) – e$B"(e(B 2 e$B"(e(B
– e$B"(e(B 5 e$B"(e(B
end)
coroutine.resume(c2) – e$B"(e(B 1 e$B"(e(B
– e$B"(e(B 6 e$B"(e(B
– e$B"(e(B e$B$3$3$G<B9T=*N;e(B
e$B"(e(B
[[test.lua e$B$3$3$^$Ge(B]]

$ lua test.lua
1

[[test.mi e$B$3$3$+$ie(B]]
MODULE test;
FROM SYSTEM IMPORT WORD, ADR, ADDRESS, TRANSFER, NEWPROCESS;
FROM InOut IMPORT WriteInt, WriteLn;
VAR main, co1, co2 : ADDRESS;
Workspace1 : ARRAY [0…1023] OF WORD;
Workspace2 : ARRAY [0…1023] OF WORD;

PROCEDURE CO1;
BEGIN
WriteInt (1, 1); WriteLn; (* e$B"(e(B 4 e$B"(e(B )
TRANSFER (co1, co2); (
co2.yield e$BAjEve(B ) ( e$B"(e(B 5
e$B"(e(B )
WriteInt (2, 1); WriteLn; (
e$BE~C#$7$J$$e(B *)
END CO1;

PROCEDURE CO2;
BEGIN
TRANSFER (co2, co1); (* co1.yield e$BAjEve(B ) ( e$B"(e(B 3
e$B"(e(B )
(
e$B"(e(B 6 e$B"(e(B )
(
e$B$3$3$G<B9T=*N;e(B *)
END CO2;

BEGIN
(* e$B"(e(B 1 e$B"(e(B )
(
co1 = Fiber.new { CO1() }; co2 = Fiber.new { CO2() } *)
NEWPROCESS (CO1, ADR (Workspace1), SIZE (Workspace1), co1);
NEWPROCESS (CO2, ADR (Workspace2), SIZE (Workspace2), co2);

TRANSFER (main, co2); (* co2.yield e$BAjEve(B ) ( e$B"(e(B 2
e$B"(e(B )
(
e$BE~C#$7$J$$e(B *)
END test.
[[test.mi e$B$3$3$^$Ge(B]]

$ mocka -c test && mocka -p test && ./test
1

Modula-2 e$B$Oe(B mocka e$B$H$$$&<BAu$G;n$7$F$_$^$7$?!#e(B
http://www.info.uni-karlsruhe.de/~modula/index.php

Lua e$B$N%(%i!<$N%F%9%He(B:
[[test2.lua e$B$3$3$+$ie(B]]
c1 = coroutine.create(function ()
print(coroutine.resume(c2)) – e$B?F$re(B resume e$B$7$h$&$H$9$ke(B
end)
c2 = coroutine.create(function ()
coroutine.resume(c1)
end)
coroutine.resume(c2)
print(coroutine.resume(c2)) – e$B;`$s$@e(B coroutine e$B$re(B
resume e$B$7$h$&$H$9$ke(B
[[test2.lua e$B$3$3$^$Ge(B]]

$ lua test2.lua
false cannot resume non-suspended coroutine
false cannot resume dead coroutine

Lua e$B$Oe(B coroutine e$B$N=*N;$G?F$K5"$C$F$$$/e(B:
[[test3.lua]]
f1 = coroutine.create(function ()
print(“f1 before”)
coroutine.resume(f2)
print(“f1 after”)
end)
f2 = coroutine.create(function ()
print(“f2 before”)
coroutine.resume(f3)
print(“f2 after”)
end)
f3 = coroutine.create(function ()
print(“f3 before”)
coroutine.yield()
print(“f3 after”)
end)
coroutine.resume(f1)
[[test3.lua e$B$3$3$^$Ge(B]]

$ lua test3.lua
f1 before
f2 before
f3 before
f2 after
f1 after

e$B>e$H;w$F$$$ke(B Ruby e$B$N%3!<%Ie(B
[[test.rb e$B$3$3$+$ie(B]]
f2 = f3 = nil
f1 = Fiber.new do
puts “f1 before”
f2.yield
puts “f1 after”
end
f2 = Fiber.new do
puts “f2 before”
f3.yield
puts “f2 after”
end
f3 = Fiber.new do
puts “f3 before”
f2.yield
puts “f3 after”
end

f1.yield
[[test.rb e$B$3$3$^$Ge(B]]


#17

e$B1sF#$G$9!#e(B

Lua e$B$Ne(B coroutine e$B$Oe(B semi-coroutine e$B$G!“e(Bcoroutine e$B4V$K?F;R4X78$,$”$j$^$7$?!#e(B
e$B!!e(BPythone$B$HF1$8$G$9$M!#e(B

Python e$B$O4X?tFb$K%l%-%7%+%k$Ke(B yield e$B$,$"$k$H$-$@$1e(B coroutine
e$B2=e(B
(e$B$H$$$&$+e(B generator e$B2=e(B)
e$B$9$k!"$H$$$&0c$$$O$"$k$h$&$G$9!#e(B

e$B!!CfESH>C<$H$H$k$+!“e(BRuby e$B$i$7$$$$$$$H$3<h$j$H$H$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e$B$N@7W$OJ#;($G!"L@3N$JEz$($b$J$$$3$H$Oe(B
e$BM}2r$7$^$7$?!#e(B[ruby-dev:30924] e$B$de(B [ruby-dev:30941]
e$B$GE,Ev$J$3$H$re(B
e$B8@$C$F$7$^$C$F$9$
$^$;$s!#e(B


#18

e$B!!$5$5$@$G$9!#e(B

Yusuke ENDOH e$B$5$s$O=q$-$^$7$?e(B:

e$BD9J8<:Ni$7$^$9!#e(BLua e$B$He(B Modula-2 e$B$r$A$g$C$H$@$1;n$7$F$_$?$N$GJs9p$7$^$9!#e(B
e$B$O$8$a$F$Ne(B Lua e$B$He(B Modula-2 e$B$J$N$G4V0c$$$,$"$C$?$i$4$a$s$J$5$$!#e(B
e$B8!>Z$7$?%3!<%I$OKvHx$K:$;$^$9!#e(B

Lua e$B$Ne(B coroutine e$B$Oe(B semi-coroutine e$B$G!“e(Bcoroutine e$B4V$K?F;R4X78$,$”$j$^$7$?!#e(B

e$B!!$&$&!"$=$&$$$($P$=$&$G$7$?!#e(B

e$B<B9T$r0$9%3%^%s%I$,Fs<oN`e(B (resume e$B$He(B yield) e$B$"$j$^$9!#e(B
resume e$B$GB>$Ne(B coroutine e$B$r6nF0$7!"$=$Ne(B coroutine e$B$,e(B yield e$B$9$k$N$rBT$A$^e(B
e$B$9!#e(B
resume e$B$O?F$+$i;R$r!"e(Byield e$B$O;R$+$i?F$r6nF0$9$k46$8$G$9e(B (e$B?F$O;R$rA*$Y$^e(B
e$B$9$,!“e(B
e$B;R$O?F$rA*$Y$^$;$se(B) e$B!#e(B
coroutine e$B$,=*N;$9$k$H!”$=$Ne(B coroutine e$B$,e(B yield e$B$7$?$N$HF1$8F0:n$r$7$^$7e(B
e$B$?!#e(B
e$B?F;R4X78$r2u$9$h$&$JA+0\e(B (e$B;R$+$i?F$re(B resume e$B$9$ke(B) e$B$O%(%i!<$K$J$j$^$7$?!#e(B
e$B=*N;$7$?e(B coroutine e$B$re(B resume e$B$7$F$b%(%i!<$G$7$?!#e(B

e$B!!e(BPythone$B$HF1$8$G$9$M!#e(B

Modula-2 e$B$Ne(B coroutine e$B$O=c?he(B (?) e$B$Je(B coroutine e$B$G!"e(Bcoroutine e$BF1;N$OBPEy$Ge(B
e$B$9!#e(B
e$B=>$C$F<B9T$r0$9%3%^%s%I$O0l<oNe(B (TRANSFER) e$B$@$1$G$9!#e(B TRANSFER e$B$GB>$Ne(B coroutine e$B$r6nF0$7$^$9!#e(BTRANSFER e$B$7$?e(B coroutine e$B$OB>$Ne(B coroutine e$B$+$ie(B TRANSFER e$B$5$l$k$^$G@dBP$KF0$-$^$;$s!#e(B coroutine e$B$,=*N;$9$k$H%W%m%0%i%$4$H=*N;$7$^$7$?!#e(B

Lua e$B<0$He(B Modula-2 e$B<0$N$I$A$i$,$$$$$+$O0lD90lC;$@$H;W$$$^$9!#e(B

e$B!!$O$$!#e(B

1.9 e$B$Ne(B Fiber e$B$Oe(B Lua e$B$He(B Modula-2 e$B$NCf4Ve(B (e$B$d$de(B Modula-2 e$B4s$je(B) e$B$K$$$k46$8e(B
e$B$G$7$g$&$+!#e(Bcoroutine e$B=*N;;~$Ke(B yield e$B$9$k$N$Oe(B Lua e$B$C$]$$$G$9$,!"e(B
e$B<B9T$r0$9$N$,e(B 1 e$B<oN`$@$1$N$Oe(B Modula-2 e$B$C$]$$$G$9!#e(B

e$B!!$=$N$H$*$j$G$9!#$=$N0U?^$G:n$j$^$7$?!#e(B

Lua e$B$Oe(B resume e$B$He(B yield e$B$N6hJL$K$h$C$Fe(B coroutine e$B$N?F;R4X78$,J]>Z$5$l$F$*e(B
e$B$j!“e(B
coroutine e$B$N=N;;~$N5sF0e(B (e$B?F$KLa$ke(B) e$B$,4X?t8F$S=P$7$C$]$/$F$o$+$j$d$9$$$Ge(B
e$B$9!#e(B
Modula-2 e$B$O?F$b;R$b$J$/<B9T$O$?$@0lK$G!"$=$l$,ES@Z$l$?$iA4It=
$o$j!”$He(B
e$B$$$&!“e(B
e$BMpK=$J5sF0$G$O$”$j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B>$N8@8l$r8+$F$_$k$H!Ve(BFibere$B!J$N$h$&$J$b$N!K$,=$o$k!W$C$F$$e(B
e$B$&$N$O!VNc30E
$JA`:n!W$J$N$G!"!V5$$r$D$1$F$M!W$G$b$$$$$h$&$J5$e(B
e$B$O$7$^$9!#e(B


#19

e$B1sF#$G$9!#e(B

e$B?F;R4X78$rMxMQ$7$?%W%m%0%i%_%s%0$r$7$F$$$k>l9g!"!V?F;R4X78$Ne(B
e$BGK2u$r5/$3$5$J$1$l$P!W!"e(BFiber#prev e$B$,@5$7$/!V8F$S=P$785!W$r;Xe(B
e$B$7<($9$?$a!"e(BSemiCoroutine e$B$N$h$&$J;H$$J}$,2DG=$G$9!#e(B

e$B:#$Ne(B Fiber e$B$G!V?F;R4X78$r2u$5$J$$HO0O!W$H$$$&$N$O$b$N$9$4$/e(B
e$B69$$$H;W$$$^$9!#e(Bcoroutine e$B$NCf$Ge(B coroutine
e$B$r8F$S=P$7$Fe(B
suspend e$B$r<u$1$k$@$1$G0cH?$7$F$7$^$$$^$9!#e(B

e$B$5$5$@$5$s$Ne(B Generator e$B$NNc$b!"e(Bgenerator e$B$NCf$Ge(B
generator e$B$r8F$V$H0cH?$7$F$$$k5$$,$7$^$9e(B
(e$B$A$c$s$HDI$C$F$J$$$N$G4*0c$$$@$C$?$i$9$_$^$;$se(B) e$B!#e(B

show Generator.new{|g|
g.yield 10
show Generator.new{|g2|
g2.yield 100
g2.yield 200
g2.yield 300
}
g.yield 20
g.yield 30
}

$ ./test.rb
100
200
300
./test.rb:31:in initialize': unhandled exception from ./test.rb:79:innew’
from ./test.rb:79:in `’

e$B?F$r<+NO$G4IM}$7$F!"e(BFiber.new e$B$N%V%m%C%/$N:G=i$Ge(B

@parent = Fiber.prev || Fiber.root

e$B$G?F$rJ]B8$7$F!"e(removed_email_address@domain.invalid e$B$NBe$o$j$Ke(B

@parent.yield

e$B$r$9$l$PF0$$$?$h$&$J5$$,$7$^$9$,!"$^$@4V0c$C$F$k5$$,$7$^$9!#e(B
Fiber.new e$B$N%V%m%C%/$N:G8e$G$be(B @parent.yield
e$B$7$J$$$H%@%a$J5$$be(B
e$B$7$^$9$,!":#$N$H$3$mF0$+$J$$Nc$O8+$D$1$i$l$F$$$^$;$s!#$"$H!"e(B

g = Generator.new{|g|
g.yield 10
g.yield 20
g.yield 30
}

Fiber.new do
show g
end.yield

e$B$,2?$b=PNO$5$l$J$$$H$$$&$h$/$o$+$i$J$$5sF0$r$7$^$9$,!"e(B
e$B$3$l$be(B Fiber
e$B$r$h$/$o$+$C$F$J$$$3$H$,860x$N!VIT;W5D$J$3$H!W$G$7$g$&$+!#e(B

e$BM>CL$G$9$,!“8=:_$Ne(B Ruby e$B$Ne(B Continuation e$B$O7g4YIJ$G!”$^$H$b$Ke(B
e$BMxMQ$G$-$J$$%7%m%b%N$G$9!#6qBNE*$K$O!“e(Bdynamic-wind e$BAjEv$N5!G=e(B
e$B$,$”$j$^$;$s!#$J$N$G!"!Ve(Bxx e$B$NBe$o$j$Ke(B Continuation e$B$rMxMQ!W$He(B
e$B$$$&A*Br;h$O$"$j$($J$$$H9M$($F$$$^$9!#e(B

e$B$3$l$O!“BPEy$J4X78$OI,MW$J$N$+$J$!!”$H$$$&$$$$2C8:$J5$;}$A$+$ie(B
e$BE,Ev8@$$$^$7$?!#$9$$^$;$s!#e(B
e$B$A$J$
$Ke(B Lua e$B$N3+H/?X!)$NNc$NO@J8$K$h$k$H!"e(Bsemi-coroutine
e$B$Ge(B
coroutine e$B$r%(%s%3!<%I$G$-$k$H<gD%$5$l$F$^$7$?!#e(B

e$BJd=ue(B coroutine e$B$rN)$A>e$2$F!"e(BTRANSFER

e$B$9$k$H$-$OA4$F$=$Ne(B

e$BJd=ue(B coroutine e$B$r2p$7$F8F$V46$8$G$9!#%H%i%s%]%j%s!)e(B

dynamic-wind e$B$K$D$$$F$O$*;!$7$NDL$j$h$/$o$+$C$F$^$;$s$,!“Nc$($Pe(B
e$B!V%V%m%C%/$r<h$k%a%=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ine e$B$N$h$&$J;H$$J}$,2DG=$G$9!#e(B

e$B!!e(BFiber
e$B<+BN$NBPEy$J4X78$rMxMQ$7$?%W%m%0%i%_%s%0!JJ#;($J>uBVA+e(B
e$B0$r9T$&>l9g!K$G$O!"=*N;;~$K$O<+L@$G$O$J$$$N$G!V5$$r$D$1$k!WI,e(B
e$BMW$,$"$k$H;W$$$^$9!#e(B

e$B!!$?$@$7!">e5-%W%m%0%i%_%s%0$r%5%]!<%H$9$k$h$&$J!“Nc30$J$I$rEje(B
e$B$2$k5!G=$rDI2C$9$k$3$H$O$G$-$^$;$s!#$?$H$($P!“e(BPython e$B$G$Oe(B
Generator e$B$NF~$l;R!”$D$^$j?F;R4X78$NGK2u$r9T$*$&$H$9$k$HNc30$,e(B
e$BH/@8$7$^$9!#e(BFiber
e$B$N>l9g$O!”$3$l$r5v$7$?$$$?$a5vMF$7$F$$$^$9!#e(B
e$B!J$3$NJU$,CfESH>C<!Ke(B

e$B!!L\E*$,0c$&$N$G!VJL%b%N!W!Je(BCoroutinee$B$He(BSemiCoroutinee$BAjEv!K$rMQe(B
e$B0U$9$k$N$,@5$7$$$N$+$b$7$l$^$;$s!#e(B

e$B!!$G$b$J$!!#e(Blambda e$B$He(B proc
e$B$NNc$b$"$k$N$G!":#$N$^$^$G$b$$$$$he(B
e$B$&$J!"e(Blambda e$B$Oe(B lambda
e$B$G:.Mp$r>7$$$F$$$k$h$&$J!J$D$^$j!"0JA0e(B
e$B$K$b=q$-$^$7$?$,!"e(Bfactory e$B$K$h$C$F@-<A$rJQ$($k!K!#e(B

e$BNc!'e(B
Coroutine < Fiber
SemiCoroutine < Fiber

e$B$b$7$/$Oe(B

e$B!!e(BCoroutine = Fibere$B!JL>A0JQ99!Ke(B
SemiCoroutine < Fiber


e$B!!M>CL$G$9$,!“8=:_$Ne(B Ruby e$B$Ne(B Continuation
e$B$O7g4YIJ$G!”$^$H$b$Ke(B
e$BMxMQ$G$-$J$$%7%m%b%N$G$9!#6qBNE*$K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(B
Root Fiber e$B$@$C$?$ie(B Thread e$B$r=*N;e(B
(3) Thread e$B$r=*N;!Je(BModula-2 / Win32e$B!Ke(B
(4) e$B!V?F!W!J8F$S=P$785!K$XNc30!Je(BLua/Pythone$B!Ke(B
e$B!!!e(B-> Coroutine e$B$H$7$F$NI=8=NO$r@)8B$5$;$kI,MW%"%je(B
Root Fiber e$B$@$C$?$ie(B Thread e$B$r=*N;e(B
(5) e$BL$Dj5A$K$7$A$c$&e(B -> (1)

e$B!!8D?ME*$K$O!"!V<g4QE*$J46A[!W$G$O$?$V$s@bF@NO$,$J$$5$$,$9$k$Ne(B
e$B$G!"<B:]$K$$$/$D$+8=<BE*$JNc$r=P$7$F!"%W%m%0%i%%s%0$r$7$F$$Fe(B
e$B$+$i9M$($k$N$,$$$$$h$&$J5$$,$7$F$$$^$9!#e(B

e$B!!$G!"<B:]$K$$$/$D$+;H$C$F$_$?Nc!Je(BGenerator /
e$B%5%s%?%/%m!<%9Lde(B
e$BBj!K$G$O!"e(BFiber e$B$r=*N;$5$;$kI,MW$,$J$+$C$?$H$$$&!#$d$C$Q$je(B
Fiber e$B$r=*N;$5$;$k$H$$$&$N$O0[>oF0:n$J$N$+!)e(B