Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

is pointer assignment always atomic?

Name: Anonymous 2012-03-01 3:17

Is pointer assignment always atomic?

Will this always work:


struct list {
  int n;
  struct list* next;
};

struct stack {
  struct list* top;
};

thread one does:
for(;;) {
  print_list(stack->top);
  // read after write race condition for stack->top.
}

thread two does:
for(int count = 0; true; count++) {
  stack->top = new_int_list(count, stack->top);
}


Will the assignments and reads to stack->top always be atomic? Will the pointer value ever be half written or something, on some architecture?

Name: Anonymous 2012-03-04 15:47

>>34
It's really a very different thing. An OS can implement a mutex with a handle and a linked list of processes that are stalled on the mutex. When the process that is holding the mutex releases it, the OS can then wake up the process at the front of the list and remove it from the queue. Then the manager that dispatches the processes will give the woken up process some time to execute, between all the other running processes.

A mutex could be implemented simply as a spin lock. And the process manager could be unaware of any mutex, and instead give every process some time to execute, even if they are just spinning on a spin lock. But this would be inefficient. Consider 100 processes all locked on the same mutex. A good portion of the CPU time will be spent letting these processes spin on this lock. Whereas with the queue of stalled processes approach, no CPU time is needed for the stalled processes. There is the overhead of managing the queue, but it can be better than the spinning.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List