线程同步使用条件变量(Condition Variables)
require 'monitor' SONGS = [ 'Blue Suede Shoes', 'Take Five', 'Bye Bye Love', 'Rock Around The Clock', 'Ruby Tuesday' ] START_TIME = Time.new def timestamp (Time.now - START_TIME).to_i end # Wait for up to two minutee between customer requests def get_customer_request sleep(120 * rand) song = SONGS.shift puts "#{timestamp}: Requesting #{song}" if song song end # Songs take between two and three minutes def play(song) puts "#{timestamp}: Playing #{song}" sleep(120 + 60*rand) end ok_to_shutdown = false # and here's our original code playlist = [] playlist.extend(MonitorMixin) plays_pending = playlist.new_cond # Customer request thread customer = Thread.new do loop do req = get_customer_request puts req break unless req playlist.synchronize do playlist << req plays_pending.signal end end end # Player Thread player = Thread.new do loop do song = nil playlist.synchronize do break if ok_to_shutdown && playlist.empty? plays_pending.wait_while {playlist.empty?} song = playlist.shift end break unless song play(song) end end customer.join ok_to_shutdown = true player.join
19: Requesting Blue Suede Shoes
Blue Suede Shoes
19: Playing Blue Suede Shoes
63: Requesting Take Five
Take Five
101: Requesting Bye Bye Love
Bye Bye Love
149: Playing Take Five
169: Requesting Rock Around The Clock
Rock Around The Clock
215: Requesting Ruby Tuesday
Ruby Tuesday
269: Playing Bye Bye Love
428: Playing Rock Around The Clock
574: Playing Ruby Tuesday
相关推荐
ruby-4-variables.mp4 ruby-5-variables-rules.mp4 ruby-6-expression.mp4
translation - Implementing-a-Thread-Safe-Queue-using-Condition-Variables实现一个线程安全队列原链接: 翻译: Scott Gu源代码:多线程代码需要一次又一次面对的一个问题是,如何把数据从一个线程传到另一个县城...
Complex Variables and Applications
Whether you've come to Ruby because of the Rails web development framework --Ruby's killer app -- or simply because it's a relatively clean, powerful and expressive language that's useful for a lot of...
Complex Variables and Applications
Calculus_of_Several_Variables.pdf
Laravel开发-laravel-variables Laravel项目中的变量/设置管理
System_Variables_en系统变量,Expert Documentation System Variables For KUKA System Software 8.1, 8.2 and 8.3
Complex Variables and Applications 7ed
Probability, Random Variables, and Stochastic Processes (McGraw-Hill Series in Electrical Engineering)
Get a List of Windows Variables
_bootstrap-variables.scss, boot strap ass file
plex variables. The task is a bit daunting. This is one of the oldest areas in mathematics, it is beautiful and compelling, and there is a plethora of material. The literature in complex variables is ...
Effect of Different Variables on Overall Vertical Jump Height
6th Brown and churchill
Probability, Random Variables and Stochastic Processes 英文第四版。 作者是Athanasios Papoulis。帕普里斯教授,他1921年出生于希腊,分别从雅典国家技术大学和美国宾夕法尼亚大学获得电子工程和数学学位。他...
operators, comments, numbers, variables, ranges, and symbols Predefined variables andglobal constants Conditional statements, method use, classes, and modules (mixins) Lists of methods ...
为给兄弟们扫盲,从网上整理的一些东东,希望对大家有帮助。 Threads Pthread Pthread Mutex Pthread Condition variables Pthread Main API
Probability, Random Variables and Stochastic Processes本书是关于概率论和随机过程的经典教材,本书是完整版的pdf,高清晰