[gameprogrammer] Re: Single producer consumer
- From: brianevans <brianevans@xxxxxxxxxxxxxxx>
- To: gameprogrammer@xxxxxxxxxxxxx
- Date: Thu, 09 Feb 2006 12:07:15 -0600
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
- Follow-Ups:
- [gameprogrammer] Re: Single producer consumer
- From: David Olofson
- References:
- [gameprogrammer] How to have hills in a tiled game
- From: Alan Wolfe
- [gameprogrammer] Single producer consumer
- From: Kevin Jenkins
Other related posts:
- » [gameprogrammer] Single producer consumer
- » [gameprogrammer] Re: Single producer consumer
- » [gameprogrammer] Re: Single producer consumer
- » [gameprogrammer] Re: Single producer consumer
- » [gameprogrammer] Re: Single producer consumer
- » [gameprogrammer] Re: Single producer consumer
- » [gameprogrammer] Re: Single producer consumer
- » [gameprogrammer] Re: Single producer consumer
- » [gameprogrammer] Re: Single producer consumer
- » [gameprogrammer] Re: Single producer consumer
- » [gameprogrammer] Re: Single producer consumer
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
- [gameprogrammer] Re: Single producer consumer
- From: David Olofson
- [gameprogrammer] How to have hills in a tiled game
- From: Alan Wolfe
- [gameprogrammer] Single producer consumer
- From: Kevin Jenkins