Created
May 31, 2011 20:11
-
-
Save alinpopa/1001177 to your computer and use it in GitHub Desktop.
Ruby instance_eval
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'thread' | |
class Context | |
class << self | |
def evaluate(&block) | |
self.new.instance_eval(&block) | |
end | |
end | |
def self.execute(&block) | |
self.new.instance_eval(&block) | |
end | |
def initialize | |
@mutex = Mutex.new | |
end | |
def sync(&block) | |
@mutex.synchronize{ | |
yield | |
} | |
end | |
def par(no_of_threads=1,&block) | |
threads = [] | |
no_of_threads.times{ | |
threads << Thread.new{ | |
yield | |
} | |
} | |
threads.each{|t| t.join} | |
end | |
end | |
Context.execute { | |
par(10) { | |
sync { | |
puts "test" | |
} | |
} | |
} | |
Context.evaluate { | |
par(10) { | |
sync { | |
puts "test" | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment