Simon Kröger
7/10/2006 9:04:00 PM
Wes Gamble wrote:
> Wes Gamble wrote:
>> All,
>>
>> I'm reading through the Pickaxe book on synchronization and threading.
>> I want to have a method that will basically put a MAX() value from a
>> table and I want to ensure that two threads don't execute this code at
>> the same time.
>>
>> I'm thinking I can just have my object descend from Monitor (< Monitor)
>> and then put "synchronize" around the relevant code, like so:
>>
>> class myClass < Monitor
>> ...
>>
>> def get_max_value
>> synchronize { max_value = do_select_to_get_max_value }
>> max_value
>> end
>>
>> This should work just fine - correct?
>>
>> Thanks,
>> Wes
>
> There is an admonition in the Pickaxe book that says "In both the class
> form and when including MonitorMixin in an existing class it is
> essential to invoke super in the class's initialize method."
>
> I want my sychronization to occur in a class method. So that would seem
> to obviate the need for the super in the "initialize" block. Am I
> correct?
>
> Thanks,
> Wes
You have several problems in your code you may want to look at before thinking
about Threads:
* myClass has to be spelled MyClass (see Constants in Ruby)
* get_max_value isn't a class method (use self.get_max_value if you want such)
* max_value is only defined inside the block, so you get
"undefined local variable or method `max_value' for MyClass:Class"
* if you define it as class method the interpreter will show you
"undefined method `synchronize' for MyClass:Class" because synchronize
isn't a class method
* most of the time when the documentation is saying "it is essential to..."
it is there for a reason
I don't want to be harsh, but it would be nice to let the interpreter find
the obvious bugs it is able to find.
maybe that is what you want:
require 'thread'
class MyClass
@@mutex = Mutex.new
def self.do_select_to_get_max_value;
42
end
def self.get_max_value
@@mutex.synchronize do
return do_select_to_get_max_value
end
end
end
p MyClass::get_max_value
cheers
Simon