(Repost)
Thank you for the correction Bertram, I've never used a mailing list before and it didn't occur to me. Hopefully it's in the right place now.
I'm having a bit of a problem accessing variables in an instance of GServer.
What I would like to do is make a hash that is effectively global to the
current thread.
class TestServer < GServer
def initialize(port = 4000, *args)
super(port, *args)
end
def serve( io ) @test_hash = Hash.new
connect(io) #Defined in connect.rb
loop do
str = io.gets
parser(str,io)
end
end
end
The way that I'm doing it doesn't allow each thread to have it's own copy of @test_hash and that's my problem. I need each thread to be able to change
the data stored in the hash without affecting the data stored in the hash
for all threads. I'm sure that my understanding of scope and GServer itself
is causing my problem, but I just don't know what to do to fix it. I was
thinking that I could pass the hash as a parameter to the connect function
but that would quickly become a problem as it would need to be passed to
several other functions after that and if possible I just don't want to have
to use that many extra parameters in my functions. Any help would be
appreciated.
I'm having a bit of a problem accessing variables in an instance of GServer.
What I would like to do is make a hash that is effectively global to the
current thread.
This is just a random thought, but what about a class-level hash where
the running thread (Thread.current) is the key?
e.g.
class TestServer#..
@@thingy = {}
.....
protected
def test_hash
@@thingy[Thread.current]
end
def test_hash= m
@@thingy[Thread.current] = m
end
end
I'm not sure about locking (which is probably only relevant at the level
of test_hash=), but it may be worth a look
Just a thought.
- Arlen
···
On Tue, 2007-10-02 at 20:44 +0900, Chris Bailey wrote:
You could make test_hash a local variable and pass it as a parameter into the methods you need it in.
You could also build a new object to represent client state, instead of just using the server state object for everything. When a new client connects, build the client object (passing in your Hash and IO objects) and call some kind of run() method on the client to handle everything else.
I hope that helps.
James Edward Gray II
···
On Oct 2, 2007, at 6:44 AM, Chris Bailey wrote:
(Repost)
Thank you for the correction Bertram, I've never used a mailing list before and it didn't occur to me. Hopefully it's in the right place now.
I'm having a bit of a problem accessing variables in an instance of GServer.
What I would like to do is make a hash that is effectively global to the
current thread.
class TestServer < GServer
def initialize(port = 4000, *args)
super(port, *args)
end
def serve( io ) @test_hash = Hash.new
connect(io) #Defined in connect.rb
loop do
str = io.gets
parser(str,io)
end
end
end
The way that I'm doing it doesn't allow each thread to have it's own copy of @test_hash and that's my problem. I need each thread to be able to change
the data stored in the hash without affecting the data stored in the hash
for all threads.