Atomic operation: Difference between revisions

Added information about GCC's atomic operations.
[unchecked revision][unchecked revision]
m (Adding about cores to which the section applies and are much more popular them multiprocesor systems)
(Added information about GCC's atomic operations.)
Line 12:
 
On multiprocessor systems, ensuring atomicity exists is a little harder. It is still possible to use a lock (e.g. a [[spinlock]]) the same as on single processor systems, but merely using a single instruction or disabling interrupts will not guarantee atomic access. You must also ensure that no other processor or core in the system attempts to access the data you are working with. The easiest way to achieve this is to ensure that the instructions you are using assert the 'LOCK' signal on the bus, which prevents any other processor in the system from accessing the memory at the same time. On x86 processors, some instructions automatically lock the bus (e.g. 'XCHG') while others require you to specify a 'LOCK' prefix to the instruction to achieve this (e.g. 'CMPXCHG', which you should write as 'LOCK CMPXCHG op1, op2').
 
== Implementation ==
 
A few of the [[Synchronization Primitives]] (sub)pages have implementations or examples of atomic operations. Besides that, GCC can make your life easier because it has built-in functions for them. You can take a look at the [http://gcc.gnu.org/onlinedocs/gcc-4.4.1/gcc/Atomic-Builtins.html GCC Documentation] (the link is GCC 4.4.1 specific, but it shouldn't be hard to find the docs matching the version number of your GCC).
 
[[Category:Synchronization]]
252

edits