Your questions

Atomic?

Could you please enlighten me as to the meaning of atomic and non atomic when refering to critical sections altering data?

A non-atomic operation is one which is composed of several smaller steps. For example, copying an array of characters is a non-atomic operation; it is composed of the many smaller steps of copying individual characters. Each of these operations is non-atomic: it involves reading a character from one array and writing it to the other. On most machines, reading or writing a single character would be an atomic operation.

If an operation is non-atomic, the data which it is manipulating may be in an inconsistant state until all the smaller steps have been completed. (For example, in copying an array, half way through the operation, only half of the array has been copied.)

If an operation is 'atomic', there is no such thing as 'half way finished'. Either an atomic operation hasn't started, or it is completely finished.

If two threads are performing atomic operations on some kind of data structure---like both writing to a boolean variable---there is no danger of a race condition, because one thread cannot get 'half way' through writing the data, before the other thread starts to write.

When several threads are performing non-atomic operations on some kind of shared data structure, however, there is no guarantee that only one thread gets access to the data at a time: several threads may each be performing some non-atomic (multi-step) operation on the data, and the result may be corrupt data.

(Classic example is one process transferring money from account A to account B, while another process transfers money from account B to account A: if a transfer is not an atomic transaction, a race condition may result in both accounts losing or gaining money.)


updated 14 May, 1998