[gameprogrammer] Re: Single producer consumer

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.

So if the compiler did "the right thing" it seems to me like it will work, but you couldn't guarantee that it will work over all compilers or all chip architectures.

Its been several years since my OS and ASM classes, so some salt possibly required.

brian.



At 11:21 AM 2/9/2006, you wrote:
We're having a debate at work right now as to if you can write a thread-safe single producer consumer class without critical section locks. I claim you can, while another programmer claims you cannot. This is what I use:

http://www.rakkar.org/sourcecode/SingleProducerConsumer.h.txt

What I do is have two pointers - a write pointer and a read pointer. They cannot cross each other. Each pointer is only modified by one thread. Both threads compare these pointers using the == operator.

There are a few articles on google claiming correctly you cannot use a single shared pointer or index where both threads modify that variable, but none use my approach where I have two variables and each is only updated by its own thread.


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




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


Other related posts: