0
   

Operating Systems

 
 
Reply Wed 20 Mar, 2019 09:00 pm
Is this implementation free from race conditions?

typedef struct node
{
value_t data;
struct node *next;
}

Node;
Node *top; // top of stack

void
push (value_t item)
{
Node *old_node;
Node *new_node;
new_node = malloc (sizeof (Node));
new_node->data = item;

do
{
old_node = top;
new_node->next = old_node;
}

while (compare_and_swap (top, old_node, new_node) != old_node);
}

value_t
pop ()
{
Node *old_node;
Node *new_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;
}
  • Topic Stats
  • Top Replies
  • Link to this Topic
Type: Question • Score: 0 • Views: 147 • Replies: 0
No top replies

 
 

 
  1. Forums
  2. » Operating Systems
Copyright © 2024 MadLab, LLC :: Terms of Service :: Privacy Policy :: Page generated in 0.02 seconds on 04/24/2024 at 07:59:25