Bill Baldwin's Queue Code

As I write this (April 5, 2002) I have not yet started to write the code for a queue. I hope that I can finish this today as well.

I need to give some background. While working at POINT on one occasion I was given some code for an interface to the serial port. This code, of course, read data from the serial port, and placed it in a queue. There was also the capability of putting data into a queue, which was output to the port as fast as the port could send it.

This was implemented as a "sliding" queue. That is, items were added at the top of items in an array, and items were removed at the bottom of the array. As each item was removed, the entire array was slid down.

This caused problems because the queue could not keep up with the device at some of the faster baud rates. Initially I thought the problem may have been caused by the time required to slid the queue down, however, it was pointed out that when the memcpy instruction is used to do this the move is really quite fast.

The real speed problem is because while the queue is being slid, all threads using the queue have to be stopped, including the thread which is servicing the port. It was these pauses which probably caused the software to fail.

The solution is to use a circular queue, which I implemented. The problem is that the read queue and write queue were not abstracted out, so I had to make all the changes in both queues. To make matters worse, the same logic was copied for servicing the IrDA port. However, in that case there did not seem to be any problem, so the code was not changed.

Years ago I wrote a list system designed to handle lists. The interface was abstracted to the point that I could use any implementation that I wanted. That is, I actually wrote a linked list, an array based list, and a cross between the two. (The cross used a linked list, but kept a number of items per node.) These were all COM objects. The result was that I could program against any one of the list implementations, then, after the code was completed, choose the implementation that I desired based on tuning considerations.

I propose to do the same thing here. I will define a queue interface that will be an abstraction. This will be done as a COM object using ATL primarily as I want to implement it fast.

I, personally, do not like ATL. It puts the Class Id's in the registry where an end user could easily change them. If they are changed there, however, the COM object would just quit working rather than having the expected result. Also, and perhaps more importantly, it is difficult to reassign the GUIDs. That is, I have seen it suggested, and believe it to be true that GUIDs for a project should be assigned consecutively so that a user can look at the GUID and know what product is being used.

An alternative that I have used it to set the registry entries using a procedure---that is, one procedure which would register the COM object. This allows me to use the same copy of the GUID that I use in the program so that the GUID only appears one place, and can be easily changed. This concept was also easily extended to allow me to write a procedure which registered COM objects to become an operating system extension.

None-the-less, this project will use ATL.

I do have a few errands I need to run today, however, so it is conceivable that this may run over until tomorrow.

Finished Code

This took a little longer than anticipated---there were vary many unaticipated arrands that needed to be run on April 5. In addition, April 6 to April 14 was my vacation. In addition, it did take slightly longer than anticipated---about 2 days rather than one day.

Without further ado, here is the code:

That is all the code for the queue. In addition, I wrote a test program, which is in the "Test" directory off the directory for the queue. The code for the test program is:

In addition, there were two files generated by the wizard and placed in the "res" off the "Test" directory. I didn't change either, as I am willing to live with the icon generated by the wizard.

Download all the files in a zip file.


Last Updated September 06, 2009. Sample Code page