I*g
1 楼
Imagine the following compare_and_swap (CAS) primitive function is available
to
you. Using this function, write an integer counter class providing increment,
decrement, set and get APIs in an atomic and thread-safe way in a context
subject to
periodic highly intensive concurrent activity .
/* In a critical section, if (*dest) == old_val, then updates dest to new_
val and returns true otherwise leaves dest unchanged and returns false.
*/
bool compare_and_swap(int* dest, int old_val, int new_val)
to
you. Using this function, write an integer counter class providing increment,
decrement, set and get APIs in an atomic and thread-safe way in a context
subject to
periodic highly intensive concurrent activity .
/* In a critical section, if (*dest) == old_val, then updates dest to new_
val and returns true otherwise leaves dest unchanged and returns false.
*/
bool compare_and_swap(int* dest, int old_val, int new_val)