[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Using a Class (not an instance) into threads

Iñaki Baz Castillo

2/4/2009 12:40:00 PM

Hi, i'm using Ragel parser generating Ruby code. The generated code
uses a Class instead of an instance, and that class has lots of
attributes and methods. Something like:

--------------
class MyParser

class << self
attr_accessor :_machine_trans_keys
private :_machine_trans_keys, :_machine_trans_keys=3D
end

self._machine_trans_keys =3D [
0, 0, 9, 34, 10, 10,
9, 32, 9, 34, -64,
126, -128, -65, -128, -65,
-128, -65, -128, -65, -128, -65,
10, 10, 9, 32, 13,
13, 10, 10, 0, 127,
0, 0, 0
]

...
...

def self.run_machine(data)
...
end

end
-------------


So I wonder how could I use this class into a threaded program. AFAIK
a singleton class is the Class itself, so if a running thread modifies
some singleton attribute then all the other threads will see that
change (error!!!).

Am I right? How could I achieve it?


Thanks a lot.



--=20
I=C3=B1aki Baz Castillo
<ibc@aliax.net>

17 Answers

Ilan Berci

2/4/2009 2:47:00 PM

0

Iñaki Baz Castillo wrote:

>
>
> So I wonder how could I use this class into a threaded program. AFAIK
> a singleton class is the Class itself, so if a running thread modifies
> some singleton attribute then all the other threads will see that
> change (error!!!).
>
> Am I right? How could I achieve it?
>

This is not an example of a "singleton class". It is merely a class
with static methods which means that you don't have to make an instance
prior to using it. If the class that Ragel populated has no state
(which I don't believe it does) then it can safely be used in a threaded
context.

If you need further examples, please check out all the class level
methods in Math

=================================================================
As for "singleton class", The class is not the object itself as you
stated, but a class created solely for that instance.

class Foo
end

f = Foo.new
def f.poo
1
end

f.class <== Foo
f.poo <=== 1
Foo.new.poo <== undefined method!


--
Posted via http://www.ruby-....

Iñaki Baz Castillo

2/4/2009 3:06:00 PM

0

2009/2/4 Ilan Berci <coder68@yahoo.com>:

> This is not an example of a "singleton class". It is merely a class
> with static methods which means that you don't have to make an instance
> prior to using it.

Thanks for pointing it out.
When defining an attribute into "class << self [..] end" it means that
it is an instance attribute of the class (since in Ruby a Class is
also an instance). I though that *this* was called "Singleton".


> If the class that Ragel populated has no state
> (which I don't believe it does) then it can safely be used in a threaded
> context.

During a method into MyParser class, there are *a lot* of attributes
whose value change, and that value is important to remain when the
function ends, so I understand that the class that Ragel populated
does have state, am I wrong?


> If you need further examples, please check out all the class level
> methods in Math

ok, I'll do.


> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
> As for "singleton class", The class is not the object itself as you
> stated, but a class created solely for that instance.
>
> class Foo
> end
>
> f =3D Foo.new
> def f.poo
> 1
> end
>
> f.class <=3D=3D Foo
> f.poo <=3D=3D=3D 1
> Foo.new.poo <=3D=3D undefined method!

I was wrong, thanks for clarifing :)




--=20
I=C3=B1aki Baz Castillo
<ibc@aliax.net>

Iñaki Baz Castillo

2/4/2009 3:18:00 PM

0

2009/2/4 Ilan Berci <coder68@yahoo.com>:

> If you need further examples, please check out all the class level
> methods in Math

Math is a module and its methods are written as Ruby C extensions. Is
there any other example? :)

--=20
I=C3=B1aki Baz Castillo
<ibc@aliax.net>

Robert Klemme

2/4/2009 4:49:00 PM

0

2009/2/4 I=F1aki Baz Castillo <ibc@aliax.net>:
> 2009/2/4 Ilan Berci <coder68@yahoo.com>:
>
>> This is not an example of a "singleton class". It is merely a class
>> with static methods which means that you don't have to make an instance
>> prior to using it.

Actually there is no such thing as a "static method". This term
belongs to Java, C++ and probably other languages. The attribute was
defined on the singleton class of instance MyParser which happens to
be a class.

So, Inaki, yes this state does only exist once and if you change it
concurrently you're in trouble.

> Thanks for pointing it out.
> When defining an attribute into "class << self [..] end" it means that
> it is an instance attribute of the class (since in Ruby a Class is
> also an instance). I though that *this* was called "Singleton".

"Singleton" is a general term denoting things where you have only one
from. A "Singleton Class" in Ruby is the class which you obtain when
doing

class <<any_object
p self # inspects singleton class
end

This does exist for every instance - regular ones as well as classes,
since classes are objects as well.

>> If the class that Ragel populated has no state
>> (which I don't believe it does) then it can safely be used in a threaded
>> context.
>
> During a method into MyParser class, there are *a lot* of attributes
> whose value change, and that value is important to remain when the
> function ends, so I understand that the class that Ragel populated
> does have state, am I wrong?

I do not know Ragel but it may well be that the class that Ragel
created has only immutable state necessary for the parser and that the
parsing related state is in instances of that class. Can you post
more of MyParser?

Cheers

robert

--=20
remember.guy do |as, often| as.you_can - without end

Robert Klemme

2/4/2009 8:33:00 PM

0

2009/2/4 I=F1aki Baz Castillo <ibc@aliax.net>:
> El Mi=E9rcoles, 4 de Febrero de 2009, Robert Klemme escribi=F3:
>> I do not know Ragel but it may well be that the class that Ragel
>> created has only immutable state necessary for the parser and that the
>> parsing related state is in instances of that class. Can you post
>> more of MyParser?
>
> There is no usage of class instances, but just class methods and class
> attributes.
>
> I attatch a Ragel generated Ruby parser (it's just a few extract of a SIP
> parser I'm building).
> Note that the Ragel generated code is from line 11 up to line 170.

I did not look too closely but it seems that the shared state is used
read only. You can easily verify yourself by simply freezing it and
see whether you get errors from that.

Cheers

robert

--=20
remember.guy do |as, often| as.you_can - without end

Iñaki Baz Castillo

2/4/2009 9:16:00 PM

0

El Mi=E9rcoles, 4 de Febrero de 2009, Robert Klemme escribi=F3:
> 2009/2/4 I=F1aki Baz Castillo <ibc@aliax.net>:
> > El Mi=E9rcoles, 4 de Febrero de 2009, Robert Klemme escribi=F3:
> >> I do not know Ragel but it may well be that the class that Ragel
> >> created has only immutable state necessary for the parser and that the
> >> parsing related state is in instances of that class. Can you post
> >> more of MyParser?
> >
> > There is no usage of class instances, but just class methods and class
> > attributes.
> >
> > I attatch a Ragel generated Ruby parser (it's just a few extract of a S=
IP
> > parser I'm building).
> > Note that the Ragel generated code is from line 11 up to line 170.
>
> I did not look too closely but it seems that the shared state is used
> read only. You can easily verify yourself by simply freezing it and
> see whether you get errors from that.

You are right! The only writtable variables are local variables inside clas=
s=20
methods!

Really thanks a lot for pointing it out :)


=2D-=20
I=F1aki Baz Castillo

Ilan Berci

2/4/2009 10:42:00 PM

0

Robert Klemme wrote:

>>> This is not an example of a "singleton class". It is merely a class
>>> with static methods which means that you don't have to make an instance
>>> prior to using it.
>
> Actually there is no such thing as a "static method". This term
> belongs to Java, C++ and probably other languages. The attribute was
> defined on the singleton class of instance MyParser which happens to
> be a class.
>
>
>
> robert

Yes.. sorry about "static" getting in there.. I come from C++ and my
terminology still sometimes gets messed up..

Please change to: "It's merely a class with class level methods...

ilan


--
Posted via http://www.ruby-....

Iñaki Baz Castillo

2/5/2009 1:45:00 PM

0

2009/2/4 I=C3=B1aki Baz Castillo <ibc@aliax.net>:
>> I did not look too closely but it seems that the shared state is used
>> read only. You can easily verify yourself by simply freezing it and
>> see whether you get errors from that.
>
> You are right! The only writtable variables are local variables inside cl=
ass
> methods!

Also, using a Class instead of instances is faster since there is no
need of "initialize" for each usage, am I right?

--=20
I=C3=B1aki Baz Castillo
<ibc@aliax.net>

Robert Klemme

2/5/2009 3:20:00 PM

0

2009/2/5 I=F1aki Baz Castillo <ibc@aliax.net>:
> 2009/2/4 I=F1aki Baz Castillo <ibc@aliax.net>:
>>> I did not look too closely but it seems that the shared state is used
>>> read only. You can easily verify yourself by simply freezing it and
>>> see whether you get errors from that.
>>
>> You are right! The only writtable variables are local variables inside c=
lass
>> methods!
>
> Also, using a Class instead of instances is faster since there is no
> need of "initialize" for each usage, am I right?

In theory yes. But IMHO the primary reason for stuffing something into
the class should not be performance but the appropriateness in terms
of design. If it is something that all class instances must share it
belongs into the class.

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end

Simon Krahnke

2/6/2009 6:15:00 AM

0

* Iñaki Baz Castillo <ibc@aliax.net> (14:44) schrieb:

> 2009/2/4 Iñaki Baz Castillo <ibc@aliax.net>:
>>> I did not look too closely but it seems that the shared state is used
>>> read only. You can easily verify yourself by simply freezing it and
>>> see whether you get errors from that.
>>
>> You are right! The only writtable variables are local variables inside class
>> methods!
>
> Also, using a Class instead of instances is faster since there is no
> need of "initialize" for each usage, am I right?

I don't quite understand that question. Classes are instances, too. And
objects without state don't need an initialize method.

mfg, simon .... l