Skip to main content
gamedev.net
🔒 Locked

Lockfree Queue

Started by Naxupilamix Apr 12, 2007 at 4:54 AM 3 replies 1.1k views
Original Post
Naxupilamix
Naxupilamix
Hi all, I'm reading your forums for a long time now, but this is my first post here. I'm interesting in Lockfree stack or queue and i've read the post on the lockless link list. After long research on the net I've found this code: http://mooproductions.blogspot.com/feeds/posts/default #ifndef _LOCKLESS_QUEUE_ #define _LOCKLESS_QUEUE_ //******************************************************************************* // Copyright (C) 2006 Casey Borders, Moo Productions // Contact: Casey.Borders@MooProductions.org // // This software is provided 'as-is', without any express or implied warranty. // In no event will the authors be held liable for any damages arising from // the use of this software. // // Permission is granted to anyone to use this software for any purpose, // including commercial applications, and to alter it and redistribute it // freely, subject to the following restrictions: // // * The origin of this software must not be misrepresented; you must not // claim that you wrote the original software. If you use this software in a // product, an acknowledgment in the product documentation would be // appreciated but is not required. // // * Altered source versions must be plainly marked as such, and must not be // misrepresented as being the original software. // // * This notice may not be removed or altered from any source distribution. //******************************************************************************** template class LocklessQueue { private: T* mBuffer; size_t mHead; size_t mTail; int mSize; public: LocklessQueue(int size) : mSize(size+1), mBuffer(0), mHead(0), mTail(0) { mBuffer = new T[mSize]; } ~LocklessQueue() { delete[] mBuffer; } bool push(T msg) { size_t head = mHead; if((head+1)%mSize == mTail) return false; mBuffer[head++] = msg; if(head == mSize) head = 0; mHead = head; return true; } bool pop(T *msg) { size_t tail = mTail; if(tail == mHead) return false; *msg = mBuffer[tail++]; if(tail == mSize) tail = 0; mTail = tail; return true; } }; #endif Can you tell me what you think about it ? Thanks for all.
nmi
nmi
First thing is, that you should use source tags.

Second thing is, that LocklessQueue does not work if there is more than one reader or more than one writer. It does also assume that reading/writing a value of size_t is an atomic operation (which may not be the case).
Vorpy
Vorpy
If there is more than one thread accessing this structure at the same time, the result will be completely undefined and/or platform dependent (even with only one reader thread and one writer thread at a time). Threading is outside the scope of standard C++ and there's no effort made in this code to use any sort of threading constructs. This code is only suitable for single threaded use, and calling it lockless is unnecessary and deceptive. It's an ordinary fixed size circular queue.
cobru
cobru
Hey, maybe is Lockless as in "don't know no locks, don't use such things" :)

anyway, a google search after "lock-free queue" or "lock-free data structure" should reveal some pretty interesting implementations (most of them I think are based on atomic Compare-And-Swap support)
example of what google can reveal:
some C# implementation of lock-free data structures
Q: How many programmers does it take to write a nice piece of software?A: MORE.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.