Problems with agent

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Problems with agent

Itamar
This post has NOT been accepted by the mailing list yet.
Hi,

I'm playing with the following Agent code:

<code>
class Point {

    int x,y

}

agent = new Agent(new Point())


final Thread t = Thread.start {while (true)
                                {def val = agent.val;
                                 print "${val.x}, ";
                                 sleep 200;
                                 println  "$val.y ";}}
agent.send{it.x=5;sleep 100;it.y=7}
agent.send{it.x=10;sleep 100;it.y=12}
</code>

with this code I may get a dirty read of x=10; y=7 or so.

I may be missing something, but it looks like once getting the 'val' attribute from the agent, it is not thread safe at all and does not guarantee the correctness of the values inside. I understand that getValue() will block until changes are committed, but once I have it the values may still change while I'm reading the values one by one; what's the point here? (I think this problem can also be demonstrated in the shopping cart example of the GPars user guide).

furthermore, once val is obtained, the caller may change its values directly, bypassing all safety. I don't get the point here either.

now I'm confused again - what is the use case for using Agent?