Click here to Skip to main content
15,895,746 members
Articles / General Programming / Threads

A Standard Multi-threaded Dynamic Queue

Rate me:
Please Sign up or sign in to vote.
4.79/5 (24 votes)
28 Oct 2010GPL36 min read 152.2K   5.6K   111  
This is a standard Windows / C++ implementation of a multi-threaded queue.
#ifndef MULTITHREADSINGLEQUEUE_H
#define MULTITHREADSINGLEQUEUE_H

/*
   Multi Threading Queue After:
   M. Michael and M. Scott. "Nonblocking algorithms and preemption-safe locking on
                             multiprogrammed shared - memory multiprocessors."
      Journal of Parallel and Distributed Computing, 51(1):1�26, 1998.
*/

#if _MSC_VER > 1000
#pragma warning (disable: 4786)
#pragma warning (disable: 4748)
#pragma warning (disable: 4103)
#endif /* _MSC_VER > 1000 */

#include <afx.h>
#include <afxwin.h>

class CNode
{
public:
   CNode(void) { pNextNode = NULL; pValuePointer = NULL; };
   ~CNode(void) {};

   CNode* pNextNode;
   void*  pValuePointer;

private:
   // Don't allow these sorts of things
   CNode( const CNode& ) {};
   CNode& operator = ( const CNode& ){ return( *this ); };
};

class CMultiThreadSingleQueue
{
public:
   CMultiThreadSingleQueue(void);
   virtual ~CMultiThreadSingleQueue(void);

private:
   // Don't allow these sorts of things
   CMultiThreadSingleQueue( const CMultiThreadSingleQueue& ) {};
   CMultiThreadSingleQueue& operator = ( const CMultiThreadSingleQueue& ){ return( *this ); };

protected:
   // Critical sections guarding Head and Tail code sections
   CCriticalSection m_HeadCriticalSection;
   CCriticalSection m_TailCriticalSection;
   // The queue, two pointers to head and tail respectively
   CNode* pHeadNode;
   CNode* pTailNode;
   // Queue size
   volatile long m_Size;

public:
   // Enqueue
   bool Push(void* pNewValue);
   // Dequeue, pass a pointer by reference
   bool Pop (void*& pValue);
   // for accurate sizes change the code to use the Interlocked functions calls
   long GetSize() { return m_Size; }
};

#endif // ! defined(MULTITHREADSINGLEQUEUE_H)

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Software Developer (Senior)
Italy Italy
Senior Software Developer in C/C++ and Oracle.
Ex-physicist holding a Ph.D. on x-ray lasers.

Comments and Discussions