$s = $s + 1 +=====================================================+ | CPU | +--------------------------+--------------------------+ | thread 1 | thread 2 | +==========================+==========================+ | - ... | | | - copy $s into temp | | | - $s is locked | | | - copy $s into temp | | | - $s is unlocked | | | - add one to temp | | +--------------------------+--------------------------+ | | - ... | | | - copy $s into temp | | | - $s is locked | | | - copy $s into temp | | | - $s is unlocked | | | - add one to temp | | | - assign temp to $s | | | - $s is locked | | | - assign temp to $s | | | - $s is unlocked | | | - ... | +--------------------------+--------------------------+ | - assign temp to $s | | | - $s is locked | | | - assign temp to $s | | | - $s is unlocked | | | - ... | | +==========================+==========================+ => $s is wrong <= #### \$s +=====================================================+ | CPU | +--------------------------+--------------------------+ | thread 1 | thread 2 | +==========================+==========================+ | - ... | | | - read $s's refcount | | | - $s is locked | | | - refcount is read | | | - $s is unlocked | | | - add one | | +--------------------------+--------------------------+ | | - ... | | | - read $s's refcount | | | - $s is locked | | | - refcount is read | | | - $s is unlocked | | | - add one | | | - write $s's refcount | | | - $s is locked | | | - write $s's refcount | | | - $s is unlocked | | | - ... | +--------------------------+--------------------------+ | - write $s's refcount | | | - $s is locked | | | - write $s's refcount | | | - $s is unlocked | | | - ... | | +==========================+==========================+ => refcount is wrong <=