Active waiting

from Wikipedia, the free encyclopedia

Active waiting (also delay loop ; English busy waiting or spinning ) describes an activity of a computer program with which the time until a condition is fulfilled is active, i.e. H. by executing instructions which do not change the state of the program.

implementation

Active waiting by means of repeated instructions to be executed, it is checked with which whether the condition is satisfied, implements :

solange (Bedingung b nicht erfüllt) {
   tue nichts;
}

Active waiting obviously uses processor capacity for the immediate, repeated check whether the condition is fulfilled. In its pure form, no further action is taken other than checking.

Applications

Time bridging

Active waiting has often been used to bridge a period of time of a given length. The length of the time span to be bridged was converted by the programmer into a number of iterations to be run through , taking into account the execution time of an iteration of the loop:

int i = 0;
while (i < n) {
   i = i + 1;
}

Modern programming uses hardware timers to wait a certain amount of time; active waiting is only used if no such timer is available or if the waiting period is very short.

synchronization

Active waiting to synchronize the activities of parallel running / working components of a computing system is used more frequently than for bridging time . The components are either just software components ( processes or threads ) or software and hardware components that cooperate with each other. In synchronization situations, the sequence in which the actions of the components are carried out is regulated, since any parallel or temporally interlaced execution of the actions is undesirable.

The use of active waiting for synchronization is common in the following situations:

Status query
A component A (a software component) can only continue with its actions when a component B (often a device) has reached a certain state. The achievement of the status in component B is indicated in a way that allows a check by component A (see Memory Mapped I / O ). Component A then uses active waiting to prevent it from already taking action, although the state of component B does not yet allow this. Active waiting is then also called polling .
It is often sufficient not to carry out the status query continuously:
solange (Bedingung b nicht erfüllt) {
   warte für einige Zeit;
}
This variant of active waiting is also known as slow busy waiting or lazy polling . However, a prerequisite for a beneficial use is that the released processor capacity is used with the help of an operating system or a runtime environment in order to have another program executed by the processor. A disadvantage of the variant, in addition to the still existing, albeit reduced, waste of processor capacity, is that the wait is often longer than necessary because the condition is only checked again after the waiting time has expired.
Query a lock
If a shared variable is specifically agreed to synchronize the activities of software components ( processes or threads ) and a change in the variable value indicates that a process / thread can continue with its actions, the process / thread uses active waiting, to recognize the change:
Gemeinsam von A und B genutzte Variable: lock
Interpretation des Werts:                0 gesperrt, ungleich 0 offen 
Initialisierung:                         lock = 0
       
Prozess A                    Prozess B
   ...                          ...
   solange (lock == 0) {        ...
      ;                         lock = 1;
   }                            ...
   Aktion a                     ...
Since the variable lockprevents the process / thread A a uncontrollably continues the action by B, the variable lock variable (will lock ) called. Since the change in the variable is determined by means of repeated (rotating) queries, it is also referred to as a spin lock . Spinlocks are a fundamental concept of process synchronization .

See also