Sha256: 80123cd52e0b0905842b79346810a28ddfe5b8893e7de144f916e665775f9564

Contents?: true

Size: 752 Bytes

Versions: 14

Compression:

Stored size: 752 Bytes

Contents

require "thread"

module Stud
  class Task
    def initialize(*args, &block)
      # A queue to receive the result of the block
      # TODO(sissel): Don't use a queue, just store it in an instance variable.
      @queue = Queue.new

      @thread = Thread.new(@queue, *args) do |queue, *args|
        begin
          result = block.call(*args)
          queue << [:return, result]
        rescue => e
          queue << [:exception, e]
        end
      end # thread
    end # def initialize

    def wait
      @thread.join
      reason, result = @queue.pop

      if reason == :exception
        #raise StandardError.new(result)
        raise result
      else
        return result
      end
    end # def wait
  end # class Task
end # module Stud

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
stud-0.0.17 lib/stud/task.rb
stud-0.0.16 lib/stud/task.rb
stud-0.0.15 lib/stud/task.rb
stud-0.0.14 lib/stud/task.rb
stud-0.0.13 lib/stud/task.rb
stud-0.0.12 lib/stud/task.rb
stud-0.0.11 lib/stud/task.rb
stud-0.0.10 lib/stud/task.rb
stud-0.0.8 lib/stud/task.rb
stud-0.0.7 lib/stud/task.rb
stud-0.0.6 lib/stud/task.rb
stud-0.0.5 lib/stud/task.rb
stud-0.0.2 lib/stud/task.rb
stud-0.0.1 lib/stud/task.rb