开发者

Thread Locking in Ruby (use of soap4r and QT)

开发者 https://www.devze.com 2022-12-08 08:01 出处:网络
[EDIT NOTE: Noticed I had put the mutex creation in the constructor.Moved it and noticed no change.] [EDIT NOTE 2: I changed the call to app.exec in a trial run to

[EDIT NOTE: Noticed I had put the mutex creation in the constructor. Moved it and noticed no change.]

[EDIT NOTE 2: I changed the call to app.exec in a trial run to

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

I noticed that once the Soap4r service started running no process events ever got called again]

[EDIT NOTE 3: Created an associated question here: Thread lockup in ruby with Soap4r

I'm attempting to write a ruby program that receives SOAP commands to draw on a monitor (thus allowing remote monitor access). I've put together a simple test app to prototype the idea. The graphic toolkit is QT. I'm having what I assume is a problem with locking. I've added calls to test the methods in the server in the code shown. The server side that I'm testing right now is:

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
 开发者_如何学Python  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

Now when I run this I get the following output:

Attempting server start

Starting?

received call

0

#<Qt::MainWindow:0x60fea28>

in lock

received call

0

#<Qt::MainWindow:0x60fea28>

This:1--

in lock

This:2--

out lock

At that point I hang rather than recieving the total of five additions I expect. Qt does display the squares defined by "createWindow(10,0,10,10)" and "createWindow(10,10,10,10)". Given that "This:1--" and "This:2--" show within a nexted in/out lock pair I'm assuming I'm using mutex horribly wrong. This is my first time with threading in Ruby.

0

精彩评论

暂无评论...
验证码 换一张
取 消