Trying to figure out thread safety

G

Guest

(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/britneyspears/
 
R

Robert Klemme

(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?MultiThreading as well.

Regards

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top