Ruby Semaphores? [英] Ruby Semaphores?

查看:65
本文介绍了Ruby Semaphores?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究Ruby中"Fair Barbershop"问题的实现.这是用于课堂作业,但我不需要任何讲义.我一直在疯狂搜索,但似乎找不到找到与C语言中的信号相似的Ruby实现的信号量.

I'm working on an implementation of the "Fair Barbershop" problem in Ruby. This is for a class assignment, but I'm not looking for any handouts. I've been searching like crazy, but I cannot seem to find a Ruby implementation of Semaphores that mirror those found in C.

我知道这里有Mutex,那太好了.单一实现,完全可以实现这种信号量.

I know there is Mutex, and that's great. Single implementation, does exactly what that kind of semaphore should do.

然后是条件变量.我以为这样做会很好,但是看着这些,每次等待呼叫都需要使用互斥对象,在我看来,我无法将数字值添加到信号量中(例如,我有7个理发店,3个理发店,3个理发店,3个理发店,3个理发店,3个理发店,3个理发店,3个理发店).理发师等).

Then there's Condition Variables. I thought that this was going to work out great, but looking at these, they require a Mutex for every wait call, which looks to me like I can't put numerical values to the semaphore (as in, I have seven barbershops, 3 barbers, etc.).

我认为我需要一个Counting Semaphore,但是我认为Ruby在其核心中不包含这样的类有点奇怪.谁能帮助我指出正确的方向?

I think I need a Counting Semaphore, but I think it's a little bizarre that Ruby doesn't (from what I can find) contain such a class in its core. Can anyone help point me in the right direction?

推荐答案

感谢@ x3ro提供的链接.那为我指明了正确的方向.但是,使用Fukumoto提供的实现(至少对于rb1.9.2),Thread.critical不可用.此外,我尝试用Thread.exclusive {}替换Thread.critical调用只是导致了死锁.事实证明,有一个针对Ruby的Semaphore修补程序(我已在下面链接到该修补程序)已通过使用Mutex :: synchronize {}替换Thread.exclusive {}并解决了其他一些问题,从而解决了该问题.感谢@ x3ro向正确的方向推动我.

Thanks to @x3ro for his link. That pointed me in the right direction. However, with the implementation that Fukumoto gave (at least for rb1.9.2) Thread.critical isn't available. Furthermore, my attempts to replace the Thread.critical calls with Thread.exclusive{} simply resulted in deadlocks. It turns out that there is a proposed Semaphore patch for Ruby (which I've linked below) that has solved the problem by replacing Thread.exclusive{} with a Mutex::synchronize{}, among a few other tweaks. Thanks to @x3ro for pushing me in the right direction.

http://redmine.ruby-lang.org/attachments/1109/final-semaphore.patch

这篇关于Ruby Semaphores?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆