Re: xchg vs lock

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



To be noted is the xchg is used to implement the atomic operation,
another closely related instruction is the CAS - compare and swap
instruction - which is used to implement lock-free algorithms :-).

To quote from here:

http://en.wikipedia.org/wiki/Lock-free_and_wait-free_algorithms

Lock-free and wait-free algorithms are written using atomic primitives
that the hardware must provide. The most notable of these is "compare
and swap" (often notated "CAS"), which takes three arguments: a memory
address, an old value, and a new value. If the address contains the
old value, it is replaced with the new value, otherwise it is
unchanged. Critically, the hardware guarantees that this "comparison
and swap" operation is executed atomically. The success of this
operation is then reported back to the program. This allows an
algorithm to read a datum from memory, modify it, and write it back
only if no other thread modified it in the meantime.

For example, consider a different implementation of the banking
program where each thread represents a virtual teller. The teller
reads the current value of the account (old value), adds an amount and
uses CAS to attempt to update the account balance. If no other thread
has modified the account balance in the intervening period, the CAS
will succeed, and the account balance will be updated. However, if a
concurrent modification has occurred, the CAS will fail, and the
teller will retry the update (by first fetching the new account
balance). Each teller will perform this CAS operation in a loop,
retrying until they are successful. This algorithm is lock-free but
not wait-free, since other threads may keep writing new values and
make the failing teller try again indefinitely.

Implementation is non-trivial though:
http://www.cl.cam.ac.uk/research/srg/netos/lock-free/

--
To unsubscribe from this list: send an email with
"unsubscribe kernelnewbies" to ecartis@xxxxxxxxxxxx
Please read the FAQ at http://kernelnewbies.org/FAQ


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux