In the independent update pattern, both of the following occur:

  • Two tasks contain regions of code that update the same memory locations.

  • It does not matter what order the code regions execute in, as long as the regions do not execute in parallel.

For example, suppose that multiple tasks call do_something():

void do_something()
{
    static bool initialized = false;
    if (!initialized) {
        do_the_initialization();
        initialized = true;
    }
    do_the_real_work();
}

The function do_something() updates the variable initialized as well as the initialized memory locations. The function do_the_real_work() will never be called before the initialization happens; and the initialization will only happen once, regardless of which task calls do_something() first, as long as two tasks do not try to execute the if statement at the same time. If two tasks do try to execute the if statement at the same time, they could both see that initialized is false and both try to do the initialization.

The following sections describe several aspects of synchronizing independent updates, including explicit locking, assigning locks, and potential problems of using synchronization.

Para obter informações mais completas sobre otimizações do compilador, consulte nosso aviso de otimização.