Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 25th, 2012  |  syntax: None  |  size: 0.48 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Why does __sync_add_and_fetch work for a 64 bit variable on a 32 bit system?
  2. value = dest;
  3. While(!CAS8B(&dest,value,value + 1))
  4. {
  5.     value = dest;
  6. }
  7.        
  8. oldValue = *dest;
  9. do {
  10.     newValue = oldValue+1;
  11. } while (!compare_and_swap(dest, &oldValue, newValue));
  12.        
  13. bool compare_and_swap (int *dest, int *oldVal, int newVal)
  14. {
  15.   do atomically {
  16.     if ( *oldVal == *dest ) {
  17.         *dest = newVal;
  18.         return true;
  19.     } else {
  20.         *oldVal = *dest;
  21.         return false;
  22.     }
  23.   }
  24. }