{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}


cs162_fa08_mt1-8 - ignore the null-pointer exception for...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
CS 162 Fall 2008 Midterm Exam I October 15, 2008 Page 10/21 An object such as a queue is considered “lock-free” if multiple processes can operate on this object simultaneously without requiring the use of locks, busy-waiting, or sleeping. In this problem, we are going to construct a lock-free FIFO queue using the atomic CAS operation. This queue needs both an Enqueue and Dequeue method. We are going to do this in a slightly different way than normally. Rather than Head and Tail pointers, we are going to have “ PrevHead ” and Tail pointers. PrevHead will point at the last object returned from the queue. Thus, we can find the head of the queue (for dequeuing). If we don’t have to worry about simultaneous Enqueue or Dequeue operations, the code is straightforward (
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ignore the null-pointer exception for the Dequeue() operation for now): // Holding cell for an entry class QueueEntry { QueueEntry next = null; Object stored; QueueEntry(Object newobject) { stored = newobject; } } // The actual Queue (not yet lock free!) class Queue { QueueEntry prevHead = new QueueEntry(null); QueueEntry tail = prevHead; void Enqueue(Object newobject) { QueueEntry newEntry = new QueueEntry(newobject); QueneEntry oldtail = tail; tail = newEntry; oldtail.next = newEntry; } Object Dequeue() { QueueEntry oldprevHead = prevHead; QueueEntry nextEntry = oldprevHead.next; prevHead = nextEntry; return nextEntry.stored; } } Problem 3d[3pts]: For this non-multithreaded code, draw the state of a queue with 2 queued items on it:...
View Full Document

{[ snackBarMessage ]}