Single producer/single consumer bounded queue

Another little snippet. I’m a big fan of bounded containers for multithreaded environment. Sure, they’re not as flexible, but at least you don’t have to worry so much about memory allocation. Most of unbounded containers rely on some kind of list, so nodes have to be managed separately, which complicates the code and actually introduces hidden locks (unless you use your own multithreaded allocator).

For specific types of applications (like games) you usually know roughly what’s maximum capacity is needed, so why shouldn’t we take advantage of this knowledge. Attached you can find single producer/single consumer queue (one thread producer, another thread consumes). It doesn’t need locks or even interlocked ops (which are rather slow on some platforms), only memory barriers (usually cheaper). Tested on x86/X360, but I wouldn’t go as far as to call it portable (too many places where compiler can screw things up). It’s caller’s responsibility not to push to full queue/pop from an empty one. Header can be downloaded here.

BTW, I used it in my recent port of MemTracer to X360, it turned out to be much easier than I expected. C# application required almost no modifications and thanks to DmCaptureStackBackTrace game side was rather easy as well.

Old comments

queue theory 2010-04-02 20:15:56

[…] theory. … Upcoming Queue | FAQ | Tips. GET NEATORAMA BY EMAIL. Email: AUTHORS' BLOGS. Ape Lad, …Single producer/single consumer bounded queue | .mischief …Another little snippet. I'm a big fan of bounded containers for multithreaded environment. Sure, […]