<分区>
谁能给我总结一下比较和交换编程的优点和缺点? (例如多核 CPU 性能)
这是 Java 中的示例:
/**
* Atomically increments by one the current value.
*
* @return the updated value
*/
public final int incrementAndGet() {
for (;;) {
int current = get();
int next = current + 1;
if (compareAndSet(current, next))
return next;
}
}
=== 编辑===
请在单核/CPU中专门讲讲。