On Friday, 3 January 2025 at 00:17:27 UTC, Guillaume Piolat wrote:
>On Thursday, 2 January 2025 at 19:59:26 UTC, Jin wrote:
>If someone can tell me what could be wrong here, I would be very grateful.
https://github.com/nin-jin/go.d/blob/master/source/jin/go/queue.d#L67
A concurrent put
and popFront
will do nothing to avoid races in Queue
.
Individual access to _offset is atomic but its point of use in both put and popFront is not.
Both functions look like this:
- Atomic-read-offset
- Anything can happen
- Atomic-write-offset
If one function has completed 1) then other has completed 1+2+3 then you get a race.
Chaining two atomic things doesn't make the whole atomic, rather classic mistake with mutual exclusion.
Its a single producer, single consumer queue, so only one thread pushes, and one thread pulls. So the "2 Anything can happen" doesnt apply, since there's only one thread actually pushing to the queue. The consumer thread only reads the producer.offset, so it cant interfere with pushes, and it only sees a pushed message once producer.offset is updated.