J
John Bob
Hi all,
I've found something a bit weird while trying to make a C extension
wrapping a custom network library :
- My extension creates a BaseClient class with a method 'receive' (it
basically wait a response from a server for a given period of time)
- I created a ruby class Client that inherits from BaseClient. In the
constructor of this class, I create a thread that loops on the method
receive(). Note that in my C code, the receive function does a
WaitForSingleObject (Windows equivalent of pthread_cond_wait ) with a
timeout of 100ms
My problem is that even if my reception thread is in a wait state, my
main thread doesn't run smoothly (it freezes while the other thread
calls Client#receive).
I tried to place a Thread.pass between each iteration of the loop,
that's a little better but still far from what I expected.
Tested on Ruby 1.8.7 and ruby 1.9.1 (thought that native threads would
solve my problem, but not at all).
My code is the following :
require 'TestClientBase'
class Client < ClientBase
def initialize()
@thread = Thread.new(self) {
|client|
fin = false
while fin != true
msg = client.receive(100)
case msg
when TIME_OUT
puts ("Timeout")
when SERV_CLOSED
fin = true
end
Thread.pass
end
}
@thread.priority = -1
end
def wait()
@thread.join(10.0)
end
end
client = Client.new
100.times {
puts "Time=#{Time.now}"
}
client.wait()
Thanks for you replies.
I've found something a bit weird while trying to make a C extension
wrapping a custom network library :
- My extension creates a BaseClient class with a method 'receive' (it
basically wait a response from a server for a given period of time)
- I created a ruby class Client that inherits from BaseClient. In the
constructor of this class, I create a thread that loops on the method
receive(). Note that in my C code, the receive function does a
WaitForSingleObject (Windows equivalent of pthread_cond_wait ) with a
timeout of 100ms
My problem is that even if my reception thread is in a wait state, my
main thread doesn't run smoothly (it freezes while the other thread
calls Client#receive).
I tried to place a Thread.pass between each iteration of the loop,
that's a little better but still far from what I expected.
Tested on Ruby 1.8.7 and ruby 1.9.1 (thought that native threads would
solve my problem, but not at all).
My code is the following :
require 'TestClientBase'
class Client < ClientBase
def initialize()
@thread = Thread.new(self) {
|client|
fin = false
while fin != true
msg = client.receive(100)
case msg
when TIME_OUT
puts ("Timeout")
when SERV_CLOSED
fin = true
end
Thread.pass
end
}
@thread.priority = -1
end
def wait()
@thread.join(10.0)
end
end
client = Client.new
100.times {
puts "Time=#{Time.now}"
}
client.wait()
Thanks for you replies.