Quick Question regards react closure - is it atomic

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

Quick Question regards react closure - is it atomic

steve.nesbitt
All:

Quick and stupid question of the day. Can I assume that react closure is
atomic? That is if I query and update a private field of that Actor that I am
not inviting a Lost Update issue?

For example:

class myActor extends AbstractPooledActor {
        private Integer counter = 0

        protected void act() {
            loop {
              react { message ->
                  if ( counter < 2 ) {
                     counter ++
                     doSomething()
                  }
              }
           }
       }

     def doSomething() {}
}


Thx!

-steve

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Quick Question regards react closure - is it atomic

Hamlet D'Arcy
I would assume that the fields in Gpars objects share the same memory
model as Groovy/Java objects.
So in this case, I would mark counter as transient or make the object
of type AtomicInteger.

... but perhaps there is some magic happening under the covers...

--
Hamlet D'Arcy
[hidden email]



On Thu, Dec 17, 2009 at 5:07 PM, Stephen Nesbitt
<[hidden email]> wrote:

> All:
>
> Quick and stupid question of the day. Can I assume that react closure is
> atomic? That is if I query and update a private field of that Actor that I am
> not inviting a Lost Update issue?
>
> For example:
>
> class myActor extends AbstractPooledActor {
>        private Integer counter = 0
>
>        protected void act() {
>            loop {
>              react { message ->
>                  if ( counter < 2 ) {
>                     counter ++
>                     doSomething()
>                  }
>              }
>           }
>       }
>
>     def doSomething() {}
> }
>
>
> Thx!
>
> -steve
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Quick Question regards react closure - is it atomic

Vaclav
Administrator
Hi Stephen,

your code will work as expected. Actors guarantee that always at most one thread processes the actor's body at a time and also under the covers the memory gets synchronized each time a thread gets assigned to an actor so the actor's state can be safely modified by code in the body without any other extra (synchronization or locking) effort.

Cheers,

Vaclav



On Thu, Dec 17, 2009 at 5:07 PM, Stephen Nesbitt
<[hidden email]> wrote:
> All:
>
> Quick and stupid question of the day. Can I assume that react closure is
> atomic? That is if I query and update a private field of that Actor that I am
> not inviting a Lost Update issue?
>
> For example:
>
> class myActor extends AbstractPooledActor {
>        private Integer counter = 0
>
>        protected void act() {
>            loop {
>              react { message ->
>                  if ( counter < 2 ) {
>                     counter ++
>                     doSomething()
>                  }
>              }
>           }
>       }
>
>     def doSomething() {}
> }
>
>
> Thx!
>
> -steve
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email





--
E-mail: [hidden email]
Blog: http://www.jroller.com/vaclav
Linkedin page: http://www.linkedin.com/in/vaclavpech
Reply | Threaded
Open this post in threaded view
|

Re: Quick Question regards react closure - is it atomic

Tom Nichols
Neato.  That's a good point to make in the actors documentation.

2009/12/18 Vaclav Pech <[hidden email]>:

> Hi Stephen,
>
> your code will work as expected. Actors guarantee that always at most one thread processes the actor's body at a time and also under the covers the memory gets synchronized each time a thread gets assigned to an actor so the actor's state can be safely modified by code in the body without any other extra (synchronization or locking) effort.
>
> Cheers,
>
> Vaclav
>
>
>>
>> On Thu, Dec 17, 2009 at 5:07 PM, Stephen Nesbitt
>> <[hidden email]> wrote:
>> > All:
>> >
>> > Quick and stupid question of the day. Can I assume that react closure is
>> > atomic? That is if I query and update a private field of that Actor that I am
>> > not inviting a Lost Update issue?
>> >
>> > For example:
>> >
>> > class myActor extends AbstractPooledActor {
>> >        private Integer counter = 0
>> >
>> >        protected void act() {
>> >            loop {
>> >              react { message ->
>> >                  if ( counter < 2 ) {
>> >                     counter ++
>> >                     doSomething()
>> >                  }
>> >              }
>> >           }
>> >       }
>> >
>> >     def doSomething() {}
>> > }
>> >
>> >
>> > Thx!
>> >
>> > -steve
>> >
>> > ---------------------------------------------------------------------
>> > To unsubscribe from this list, please visit:
>> >
>> >    http://xircles.codehaus.org/manage_email
>> >
>> >
>> >
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>>    http://xircles.codehaus.org/manage_email
>>
>>
>
>
>
> --
> E-mail: [hidden email]
> Blog: http://www.jroller.com/vaclav
> Linkedin page: http://www.linkedin.com/in/vaclavpech
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Quick Question regards react closure - is it atomic

Vaclav
Administrator
That's right. I'll put the note on a prominent place.


On Fri, Dec 18, 2009 at 1:27 PM, Tom Nichols <[hidden email]> wrote:
Neato.  That's a good point to make in the actors documentation.

2009/12/18 Vaclav Pech <[hidden email]>:
> Hi Stephen,
>
> your code will work as expected. Actors guarantee that always at most one thread processes the actor's body at a time and also under the covers the memory gets synchronized each time a thread gets assigned to an actor so the actor's state can be safely modified by code in the body without any other extra (synchronization or locking) effort.
>
> Cheers,
>
> Vaclav
>
>
>>
>> On Thu, Dec 17, 2009 at 5:07 PM, Stephen Nesbitt
>> <[hidden email]> wrote:
>> > All:
>> >
>> > Quick and stupid question of the day. Can I assume that react closure is
>> > atomic? That is if I query and update a private field of that Actor that I am
>> > not inviting a Lost Update issue?
>> >
>> > For example:
>> >
>> > class myActor extends AbstractPooledActor {
>> >        private Integer counter = 0
>> >
>> >        protected void act() {
>> >            loop {
>> >              react { message ->
>> >                  if ( counter < 2 ) {
>> >                     counter ++
>> >                     doSomething()
>> >                  }
>> >              }
>> >           }
>> >       }
>> >
>> >     def doSomething() {}
>> > }
>> >
>> >
>> > Thx!
>> >
>> > -steve
>> >
>> > ---------------------------------------------------------------------
>> > To unsubscribe from this list, please visit:
>> >
>> >    http://xircles.codehaus.org/manage_email
>> >
>> >
>> >
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>>    http://xircles.codehaus.org/manage_email
>>
>>
>
>
>
> --
> E-mail: [hidden email]
> Blog: http://www.jroller.com/vaclav
> Linkedin page: http://www.linkedin.com/in/vaclavpech
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email





--
E-mail: [hidden email]
Blog: http://www.jroller.com/vaclav
Linkedin page: http://www.linkedin.com/in/vaclavpech
Reply | Threaded
Open this post in threaded view
|

Re: Quick Question regards react closure - is it atomic

Hamlet D'Arcy
In reply to this post by Vaclav
That's interesting, I had no idea! Maybe I should read the documents
before trying to answer questions ;)

--
Hamlet D'Arcy
[hidden email]


On Fri, Dec 18, 2009 at 4:00 AM, Vaclav Pech <[hidden email]> wrote:

> Hi Stephen,
>
> your code will work as expected. Actors guarantee that always at most one
> thread processes the actor's body at a time and also under the covers the
> memory gets synchronized each time a thread gets assigned to an actor so the
> actor's state can be safely modified by code in the body without any other
> extra (synchronization or locking) effort.
>
> Cheers,
>
> Vaclav
>
>
>>
>> On Thu, Dec 17, 2009 at 5:07 PM, Stephen Nesbitt
>> <[hidden email]> wrote:
>> > All:
>> >
>> > Quick and stupid question of the day. Can I assume that react closure is
>> > atomic? That is if I query and update a private field of that Actor that
>> > I am
>> > not inviting a Lost Update issue?
>> >
>> > For example:
>> >
>> > class myActor extends AbstractPooledActor {
>> >        private Integer counter = 0
>> >
>> >        protected void act() {
>> >            loop {
>> >              react { message ->
>> >                  if ( counter < 2 ) {
>> >                     counter ++
>> >                     doSomething()
>> >                  }
>> >              }
>> >           }
>> >       }
>> >
>> >     def doSomething() {}
>> > }
>> >
>> >
>> > Thx!
>> >
>> > -steve
>> >
>> > ---------------------------------------------------------------------
>> > To unsubscribe from this list, please visit:
>> >
>> >    http://xircles.codehaus.org/manage_email
>> >
>> >
>> >
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>>    http://xircles.codehaus.org/manage_email
>>
>>
>
>
>
> --
> E-mail: [hidden email]
> Blog: http://www.jroller.com/vaclav
> Linkedin page: http://www.linkedin.com/in/vaclavpech
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Quick Question regards react closure - is it atomic

Vaclav
Administrator
Yes, you write actor's body as if there were no threads around, pure sequential code.


On Fri, Dec 18, 2009 at 1:37 PM, Hamlet D'Arcy <[hidden email]> wrote:
That's interesting, I had no idea! Maybe I should read the documents
before trying to answer questions ;)


Well, the documentation is just now getting finished, so you have an excuse ;)


Vaclav


Reply | Threaded
Open this post in threaded view
|

Re: Quick Question regards react closure - is it atomic

steve.nesbitt
In reply to this post by Vaclav
Vaclav:

Thanks for the clarification - this is very welcome news.

One more question - how are you defining "Actor's body"? Is it the entire class
or the react statement or ??

Thxs!


-steveOn Friday 18 December 2009 02:00:32 am you wrote:

> Hi Stephen,
>
> your code will work as expected. Actors guarantee that always at most one
> thread processes the actor's body at a time and also under the covers the
> memory gets synchronized each time a thread gets assigned to an actor so
>  the actor's state can be safely modified by code in the body without any
>  other extra (synchronization or locking) effort.
>
> Cheers,
>


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Quick Question regards react closure - is it atomic

Vaclav
Administrator
Hi Stephen,

ideally actor's code should never be invoked directly from outside so all the code of the actor class can only be executed by the thread handling the last received message and so all the actor's code is thread-safe.
You need to be careful, though, if you allow actor's methods to be called by other objects directly. This would break the guarantees.

Vaclav


On Fri, Dec 18, 2009 at 6:28 PM, Stephen Nesbitt <[hidden email]> wrote:
Vaclav:

Thanks for the clarification - this is very welcome news.

One more question - how are you defining "Actor's body"? Is it the entire class
or the react statement or ??

Thxs!


-steveOn Friday 18 December 2009 02:00:32 am you wrote:
> Hi Stephen,
>
> your code will work as expected. Actors guarantee that always at most one
> thread processes the actor's body at a time and also under the covers the
> memory gets synchronized each time a thread gets assigned to an actor so
>  the actor's state can be safely modified by code in the body without any
>  other extra (synchronization or locking) effort.
>
> Cheers,
>


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email





--
E-mail: [hidden email]
Blog: http://www.jroller.com/vaclav
Linkedin page: http://www.linkedin.com/in/vaclavpech