Ruby中的线程锁定(使用soap4r和QT)

[编辑注意:注意到我已将互斥锁创建放在构造函数中。 移动它并注意到没有变化。]

[编辑注2:我在试运行中将调用更改为app.exec

while TRUE do app.processEvents() puts '." end 

我注意到,一旦Soap4r服务开始运行,没有再次调用过程事件]

[编辑注释3:在这里创建了一个相关的问题: 使用Soap4r在ruby中进行线程锁定

我正在尝试编写一个ruby程序,它接收SOAP命令以在监视器上绘制(从而允许远程监视器访问)。 我已经整理了一个简单的测试应用程序来构思这个想法。 图形工具包是QT。 我有我认为锁定的问题。 我已经添加了调用来测试服务器中显示的代码中的方法。 我正在测试的服务器端是:

 require 'rubygems' require 'Qt4' require 'thread' require 'soap/rpc/standaloneserver' class Box < Qt::Widget def initialize(parent = nil) super setPalette(Qt::Palette.new(Qt::Color.new(250,0,0))) setAutoFillBackground(true) show end end class SOAPServer < SOAP::RPC::StandaloneServer @@mutex = Mutex.new def initialize(* args) super # Exposed methods add_method(self, 'createWindow', 'x', 'y', 'width', 'length') end def createWindow(x, y, width, length) puts 'received call' windowID = 0 puts @boxList.length puts @parent @@mutex.synchronize do puts 'in lock' box = Box.new(@parent) box.setGeometry(x, y, width, length) windowID = @boxList.push(box).length print "This:", windowID, "--\n" end puts 'out lock' return windowID end def postInitialize (parent) @parent = parent @boxList = Array.new end end windowSizeX = 400 windowSizeY = 300 app = Qt::Application.new(ARGV) mainwindow = Qt::MainWindow.new mainwindow.resize(windowSizeX, windowSizeY) mainwindow.show puts 'Attempting server start' myServer = SOAPServer.new('monitorservice', 'urn:ruby:MonitorService', 'localhost', 4004) myServer.postInitialize(mainwindow) Thread.new do puts 'Starting?' myServer.start puts 'Started?' end Thread.new do myServer.createWindow(10,0,10,10) myServer.createWindow(10,30,10,10) myServer.createWindow(10,60,10,10) myServer.createWindow(10,90,10,10) end myServer.createWindow(10,10,10,10) Thread.new do app.exec end gets 

现在当我运行它时,我得到以下输出:

尝试启动服务器
开始?
接到电话
0
#
锁定
接到电话
0
#
这:1–
锁定
这:2–
出锁

那时我挂起而不是收到我期望的总共五个新增内容。 Qt显示由“createWindow(10,0,10,10)”和“createWindow(10,10,10,10)”定义的方块。 鉴于“This:1–”和“This:2–”显示在一个nexted in / out锁定对中,我假设我正在使用互斥锁可怕的错误。 这是我第一次使用Ruby进行线程化。