[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Trying to figure out thread safety

Thomas Morgan

10/30/2003 10:26:00 PM

(Having been told of a solution to one problem
(File::CREAT was indeed what was needed), I'll now go
off on something entirely different.)

I've been trying to figure out how thread safety
works, and mostly failing. I can sort of understand
how the examples work, but I don't quite see how to
take it and implement something useful.

Basically, I'm going to have some objects. Only one
thread should access an object at a time, and each
thread will only access one object at a time. I'd like
to be able to put something in the object that a
thread can call on, which will allow the thread to
continue when the object is available, and then the
thread can release the object so other threads can use
it.

This doesn't seem quite appropriate to use a mutex,
and ConditionVariable, which sounds like it's doing
something similar, is always used within a mutex.
Also, a thread shouldn't wait until a something else
sends a signal - unless some other thread has
specifically asked for the object, it should execute
immediately.

The Semaphore library on RAA sounds like it might do
something like this, but I haven't been able to figure
out quite how it works, so I'm not certain if it does
what I want.

Can anyone explain to me how to do something like
this?

-Morgan

__________________________________
Do you Yahoo!?
Exclusive Video Premiere - Britney Spears
http://launch.yahoo.com/promos/brit...

1 Answer

Robert Klemme

10/31/2003 8:35:00 AM

0


<agemoagemo@yahoo.com> schrieb im Newsbeitrag
news:20031030222546.54735.qmail@web14006.mail.yahoo.com...
> (Having been told of a solution to one problem
> (File::CREAT was indeed what was needed), I'll now go
> off on something entirely different.)
>
> I've been trying to figure out how thread safety
> works, and mostly failing. I can sort of understand
> how the examples work, but I don't quite see how to
> take it and implement something useful.
>
> Basically, I'm going to have some objects. Only one
> thread should access an object at a time, and each
> thread will only access one object at a time. I'd like
> to be able to put something in the object that a
> thread can call on, which will allow the thread to
> continue when the object is available, and then the
> thread can release the object so other threads can use
> it.

This is best done with Mutex#synchronize which is similar to Java's
'synchronized'.

> This doesn't seem quite appropriate to use a mutex,
> and ConditionVariable, which sounds like it's doing
> something similar, is always used within a mutex.
> Also, a thread shouldn't wait until a something else
> sends a signal - unless some other thread has
> specifically asked for the object, it should execute
> immediately.

You don't need a condition var, you just need a mutex.

> The Semaphore library on RAA sounds like it might do
> something like this, but I haven't been able to figure
> out quite how it works, so I'm not certain if it does
> what I want.

Not necessary to use that. The synchronize variant is better since it
automatically ensures the lock is release if the block is left - either
normally or via exception.

> Can anyone explain to me how to do something like
> this?

It depends what you are up to. If you only want to invoke severa
operations on the same instance a Mutex suffices:


require 'thread'

class Resource
attr_reader :lock

def initialize
@lock = Mutex.new
end
end

class Demo < Resource
def method1
puts "#{Thread.current['name']} invoked method1"
end

def method2
puts "#{Thread.current['name']} invoked method2"
end
end

r = Demo.new

threads = (1..5).map do |i|
Thread.new( i ) do |nr|
Thread.current['name'] = "Thread #{i}"

3.times do
r.lock.synchronize do
puts "#{nr} got lock"
# work with resource
r.method1
# simulate time and give other threads a chance:
sleep 1
r.method2
puts "#{nr} releasing lock"
end
end
end
end

threads.each {|t| t.join}


see http://www.rubygarden.org/ruby?Mult... as well.

Regards

robert