File: coroutine.rb

Project: Invitation to Ruby

#!/usr/bin/env ruby

class Coroutine
  def initialize
    callcc { |cont|
      @resume = cont
      return
    }
    yield(self)
    @done.call
  end
  def switch_to(coroutine)
    callcc { |cont|
      @resume = cont
      coroutine.resume
    }
  end
  def start
    callcc { |done|
      @done = done
      resume
    }
  end
  def resume
    @resume.call
  end
end

Used by: coroutinedemo


[ Index ][ Table of Contents ]
Generated by [ source2html ]