今天看了Ruby的线程部分。《Programming Ruby》第一版的HTML版的线程和进程部分讲得很详细。看完后感觉就好像又把操作系统的这一部分重温了一遍。尤其是Spawning New Processes那一节,如果没有学过操作系统还真不知道他说什么。

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"
end
简直和Unix课里面的fork代码示例一样,父子进程共享同一段代码。《Programming Ruby》对这段代码的解释是“There's one more twist to popen. 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 }