The compare and swap() instruction can be used to design lock-free data structures such as stacks, queues, and lists. The program example shown in Figure 6.18 presents a possible solution to a...


The compare and swap() instruction can be used to design lock-free data structures such as stacks, queues, and lists. The program example shown in Figure 6.18 presents a possible solution to a lock-free stack using CAS instructions, where the stack is represented as a linked list of Node elements with the top representing the top of the stack. Is this implementation free from race conditions?


typedef struct node {<br>value_t data;<br>struct node *next;<br>} Node;<br>Node *top; // top of stack<br>void push(valu0_t itom) {<br>Node +old_node;<br>Node +new_node;<br>new node = malloc(sizeof (Node));<br>nev node->data = item;<br>do {<br>old nodo = top;<br>now nodo->noxt = old_nodo;<br>}<br>while (compare and.swap(top, old.node,new.node) != oldnode);<br>}<br>value_t pop() {<br>Node +old node;<br>Node *nev node;<br>do {<br>old node = top;<br>if (old node == NULL)<br>return NULL;<br>new node = old node->next;<br>}<br>while (compare_and_swap(top, old_node, new node) != old_node);<br>return old node->data;<br>}<br>Figure 6.18 Lock-free stack for Exercise 6 15<br>

Extracted text: typedef struct node { value_t data; struct node *next; } Node; Node *top; // top of stack void push(valu0_t itom) { Node +old_node; Node +new_node; new node = malloc(sizeof (Node)); nev node->data = item; do { old nodo = top; now nodo->noxt = old_nodo; } while (compare and.swap(top, old.node,new.node) != oldnode); } value_t pop() { Node +old node; Node *nev node; do { old node = top; if (old node == NULL) return NULL; new node = old node->next; } while (compare_and_swap(top, old_node, new node) != old_node); return old node->data; } Figure 6.18 Lock-free stack for Exercise 6 15

Jun 08, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here