1,885
edits
Changes
Created page with 'Category:Computer Architecture An ''atomic operation'' is one that cannot be interrupted. These operations are typically used for acquiring locks. Consider the following cod…'
[[Category:Computer Architecture]]
An ''atomic operation'' is one that cannot be interrupted. These operations are typically used for acquiring locks.
Consider the following code, intended to set a "lock" in shared memory so that only one thread or process performs a particular action at a time:
if (lock == 0) {
lock++
/* Perform exclusive action */
lock--
} else {
abort()
}
If another process or thread writes to the lock variable between the lock comparison (lock == 0) and setting the lock (lock++), then the lock could fail to ensure exclusive action.
There are two ways to accomplish an atomic operation:
# Use a single machine instruction.
# Tell the operating system in advance not to interrupt the operation.
GCC provides [[Compiler Intrinsics|intrinsics]] for some atomic operations.
An ''atomic operation'' is one that cannot be interrupted. These operations are typically used for acquiring locks.
Consider the following code, intended to set a "lock" in shared memory so that only one thread or process performs a particular action at a time:
if (lock == 0) {
lock++
/* Perform exclusive action */
lock--
} else {
abort()
}
If another process or thread writes to the lock variable between the lock comparison (lock == 0) and setting the lock (lock++), then the lock could fail to ensure exclusive action.
There are two ways to accomplish an atomic operation:
# Use a single machine instruction.
# Tell the operating system in advance not to interrupt the operation.
GCC provides [[Compiler Intrinsics|intrinsics]] for some atomic operations.