|
discoLight
|
Public Member Functions | |
| CommandQueue () | |
| virtual | ~CommandQueue () |
| bool | hasItem () |
| CommandQueueEntry | dequeue () |
| void | enqueue (CommandQueueEntry value) |
Private Attributes | |
| std::queue< CommandQueueEntry > | commandQueueContents |
| sem_t | queueCountSemaphore |
| std::mutex | queueMutex |
| CommandQueue::CommandQueue | ( | ) |
This is the default constructor, which creates an instance of the queue.
This is the default constructor, which creates an empty instance of the queue.
Initialize the counting semaphore. Its count must always equal the number of items on the queue that are available to be dequeued. Because a newly created queue is empty, choose an initial value that indicates no items have been enqueued. The semaphore is shared only among the threads of this process, not between separate processes.
|
virtual |
The virtual destructor, which deallocates any allocated resources.
The destructor, which deallocates any allocated resources.
| CommandQueueEntry CommandQueue::dequeue | ( | ) |
This method will dequeue the next command from the queue. This method will block if there are no items on the queue.
This method removes and returns the next command from the front of the queue. If the queue is empty, the calling thread blocks until another thread enqueues a command.
Create a local CommandQueueEntry to hold the value that will be returned, and initialize all of its fields to zero.
Block on the counting semaphore. If no items are on the queue, this blocks the calling thread until an item is enqueued.
Lock the mutex so that no other thread can modify the queue while this thread is accessing it.
Copy the item at the front of the queue into the return value, and then remove that item from the queue.
Return the copied item.

| void CommandQueue::enqueue | ( | CommandQueueEntry | value | ) |
This method will enqueue a command on the queue. Any number can be enqueued as a command. Enqueueing a command will cause a thread blocked waiting for a command to be unblocked.
| command | This is the command that is to be enqueued. |
This method will enqueue a command on the queue. Enqueueing a command will cause a thread blocked waiting for a command to be unblocked.
| value | This is the value that is to be enqueued. |
Lock the queue.
Place the given item on the end of the queue.
Indicate that something has been enqueued through the semaphore.
Handle any other clean up you need to do.

| bool CommandQueue::hasItem | ( | ) |
This method will indicate whether or not the queue has an item that is ready to be dequeued.
This method indicates whether the queue currently contains at least one item that is ready to be dequeued. It does not block and does not remove anything from the queue. The result is only a snapshot: another thread may change the queue immediately afterwards.

|
private |
This attribute holds the contents of the queue. Items are processed in FIFO order. The internal parameters of this attribute should be used to determine if the queue is empty or not.
|
private |
This is a counting semaphore which keeps track of how many items are on the queue. It should only be used as a pure semaphore.
|
private |
This is a mutex for the class which is used to lock critical sections in different methods as is applicable.