[concurrency-interest] Basic question on making a thread waiting for 2 mailboxes...
hanson.char at gmail.com
Wed Apr 25 03:43:24 EDT 2007
>cannot rely on that if two messages arrive at the two queues
>simultaneously, both are returned because it is not the case.
If two messages arrive at the two queues simultaneously it's not guranteed
the two messages will be returned in the dequeue. It's however possible.
One can rely on the dequeue returning at least one message, but one cannot
rely on the dequeue returning 2 messages even if they arrive
simultaneously. Again, it is possible but not necessary.
On 4/24/07, Szabolcs Ferenczi <szabolcs.ferenczi at gmail.com> wrote:
> On 25/04/07, Hanson Char <hanson.char at gmail.com> wrote:
> > The assert statements presume the dequeue must happen in a certain
> > which therefore would appear to fail sometimes.
> Hi Hanson,
> just have a look at your algorithm and you see that the certain
> order is established by the algorithm so it is quite correct to
> presume, isn't it?
> callables.add(new Dequeue<T>(q1, result = new
> AtomicReferenceArray<T>(2), 0));
> callables.add(new Dequeue<T>(q2, result, 1));
> So, you assign the first position in the two element
> AtomicReferenceArray to the first queue and the second position to the
> second queue. That is what the assert statements in the test follow.
> No problem with that. The asserts happen after the dequeue() is
> returned, so the AtomicReferenceArray is stable. No race condition
> applies at that point.
> The test sometimes fails and sometimes passes, however, and it is non
> deterministic whether one element is received or two. The test also
> ensures that one element is there in each queue at the time the
> dequeue() is called. The call, on the other hand, returns one or the
> other or both. That is no problem if you are aware of it. So you
> cannot rely on that if two messages arrive at the two queues
> simultaneously, both are returned because it is not the case.
> Best Regards,
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Concurrency-interest