[gameprogrammer] Re: Single producer consumer

On Thursday 09 February 2006 19:07, brianevans wrote:
> I've only skimmed the code, but my gut feeling is that you can only 
> guarantee correct behavior if the pointer comparison is atomic.
> 
> If the compiler always keeps the pointers in registers and compares
> them, then I think you can get away with it, but what happens if its
> written to cache or memory and you have to do a load before the
> compare? 
> 
> After some minimal research, it appears that X86 does support atomic 
> load-compare-store.

All you need is atomic reads and atomic writes. Since the pointers can 
only move in one direction in the buffer ("oops, I want to unwrite 
that data" is not allowed), it doesn't matter if the other end 
changes a pointer before you're done with your copy of it. All that 
happens is that you don't see the last block of data written, or the 
last block of free space made available, respectively.

You could just retry the operation once on failure to avoid this, but 
I believe it's rather pointless, as it only enlarges your "success 
time window" by a few CPU cycles. And, in an SMP application where 
the FIFOs aren't meant to become full or empty during normal 
operation (say, you're piping data between real time threads running 
in "parallel" cycles on different CPUs), the spinning already does 
this as a side effect.


//David Olofson - Programmer, Composer, Open Source Advocate

.-------  http://olofson.net - Games, SDL examples  -------.
|        http://zeespace.net - 2.5D rendering engine       |
|       http://audiality.org - Music/audio engine          |
|     http://eel.olofson.net - Real time scripting         |
'--  http://www.reologica.se - Rheology instrumentation  --'


---------------------
To unsubscribe go to http://gameprogrammer.com/mailinglist.html


Other related posts: