61 of 158
55 queue will eventually become full. Further attempts to add data to the queue will stall until enough data is removed from the queue to make space for the new data. Although the data in queues is nominally byte oriented, you can put data of any type into a queue. All that is necessary is for the producer and consumer of the data to agree on the nature and meaning of the data. For example, the producer could copy several Single data values to a queue and as long as the consumer copies them out to Single variables all will be well. Prior to using a queue it must be initialized by using the System Library routine OpenQueue(). An example of this is shown below. Dim myQueue(1 to 40) Call OpenQueue(myQueue, SizeOf(myQueue)) This call prepares the queue for use by initializing the first 9 bytes of the specified array with queue management data. The remainder of the array is used for the data to be held by the queue, in this case the queue can hold up to 31 bytes of data since it was defined as being 40 bytes long. This implies that the smallest array usable as a queue is 10 bytes. Note the use of the SizeOf() function. Although the second parameter could just as well have been the literal value 40, if you later changed the size of the myQueue array you’d have to also remember to change the second parameter to match. If you made the queue larger, opening the queue with a smaller value would have no ill effect other than wasting RAM. On the other hand, if you reduced the size of the queue array but left the larger value as the second parameter, the data area inside the queue would overlap adjacent variables leading to puzzling results. For this reason it is highly recommend to use SizeOf() in this and other similar situations rather than hard-coded constants. An alternative method is to use a defined constant as shown below. Const myQueueSize as Integer = 40 Dim myQueue(1 to myQueueSize) Call OpenQueue(myQueue, myQueueSize) Either of these methods of improving the maintainability of your program is acceptable. Which you choose is more of a stylistic issue than a technical one. The code generated by the compiler in the two cases is identical. There are several System Library routines available for adding data to and extracting data from a queue as well as some querying functions for determining the status of the queue. Once initialized as shown above, the code fragment below will place some data in the queue. Dim punct(1 to 2) as Byte punct(1) = &H2c punct(2) = &H20 Call PutQueueStr(myQueue, "Hello") ' add a string Call PutQueue(myQueue, punct, 2) ' add some individual bytes Call PutQueueStr(myQueue, "world") ' add another string Call PutQueueByte(myQueue, &H21) ' add an exclamation point After the data is in the queue, the following code fragment will extract it and display it on the console. Do While (StatusQueue(myQueue) ' add a string Dim b as Byte Call GetQueue(myQueue, b, 1) ' retrieve a byte from the queue Debug.Print Chr(b); Loop Debug.Print