Fetch-and-add

from Wikipedia, the free encyclopedia

Fetch-and-add is a technical term in computer science that describes a method for atomically changing a memory area.

Working method

The method is implemented using a function call that is processed by the operating system. The content of a memory cell is read out and overwritten in the same memory cycle by a new value that is 1 greater. The advantage is that other processes cannot change the value of the variable during this process. Inconsistencies and data loss are therefore excluded. Appropriate hardware support must be available in order to use Fetch-and-add.

implementation

A possible implementation of the technology in the C programming language is to be given as an example. The call can differ depending on the programming language used.

<<atomic>> int FetchAndAdd(address alterWert) {
    int wert = *alterWert;
    *alterWert = wert + 1;
    return wert;
}

For this implementation, a pointer to the memory cell that is to be changed is used (here oldValue)

Differentiation from other procedures

In addition to Fetch-and-Add, there are other options for granting exclusive access to memory areas. In single-processor systems, for example, it is sufficient to prevent interrupts from being executed while the memory content is being changed by a process. However, this procedure is not sufficient on systems with several processors ( multicore architectures ).

Another alternative to fetch-and-add is Compare-and-swap .

literature