Ruby 线程--生产者、消费者

本文深入探讨了Ruby中的线程和进程概念,通过对比《ProgrammingRuby》中的描述与操作系统知识,解析了Ruby线程的实现机制及其与平台无关性的权衡。同时,文章通过一个生产者-消费者模型实例,展示了如何在Ruby中解决线程同步问题。
今天看了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 }

转载于:https://www.cnblogs.com/ydong/archive/2008/05/25/1207146.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值