IO.popen,其中的popen,我理解应该是应该是"piped open"的意思。其中这段代码,
pipe = IO.popen("-","w+")
if pipe
pipe.puts "Get a job!"
$stderr.puts "Child says '#{pipe.gets.chomp}'"
else
$stderr.puts "Dad says '#{gets.chomp}'"
puts "OK"
endpopen. If the command you pass it is a
single minus sign (``--''), popen will fork a new Ruby interpreter.
Both this and the original interpreter will continue running by returning from
the popen. The original process will receive an IO
object back, while the child will receive nil. ”。第一次看我完全没看出来他说的是什么。看了代码后一时间也没往fork去想。结果过了十分钟后灵光一现才知道是怎么回事。同志们,看英文的东西不容易啊!线程还挺好学。Ruby线程的功能是自已实现的。与操作系统无关。为了达到平台无关性,这种牺牲我觉得有点大。不说作者开发时得费多少力气。就是使用起来,也没有本地线程的种种优势。比如说线程饥饿。下面我写了一个练习性质的生产者--消费者例子。实话说,比Ruby中thread.rb里的例子要长太多……好处是,这里解决了屏幕输出时的窜行问题。
require 'thread'
class Consumer
def initialize(queue, stdout_mutex)
@queue = queue
@stdout_mutex = stdout_mutex
end
def consume
product = @queue.pop
@stdout_mutex.synchronize {
puts "Product #{product} consumed."
$stdout.flush
}
end
end
class Producer
def initialize(queue, stdout_mutex)
@queue = queue
end
def produce
product = rand(10)
@queue.push(product)
@stdout_mutex.synchronize {
puts "Product #{product} produced."
$stdout.flush
}
end
end
sized_queue = SizedQueue.new(10)
stdout_mutex = Mutex.new
consumer_threads = []
100.times {
consumer_threads << Thread.new {
consumer = Consumer.new(sized_queue, stdout_mutex)
consumer.consume
}
Thread.new {
producer = Producer.new(sized_queue, stdout_mutex)
producer.produce
}
}
consumer_threads.each { |thread| thread.join }
本文深入探讨了Ruby中的线程和进程概念,通过对比《ProgrammingRuby》中的描述与操作系统知识,解析了Ruby线程的实现机制及其与平台无关性的权衡。同时,文章通过一个生产者-消费者模型实例,展示了如何在Ruby中解决线程同步问题。

238

被折叠的 条评论
为什么被折叠?



