Synchronisation

C | Fortran-90

nowait

Definition

The nowait clause removes the implicit barrier that is present at the end of worksharing (sections, single, workshare) and target constructs.
If nowait is specified on a construct where a reduction clause is, accesses to the original list item after the end of the construct will create a data race and, thus, have unspecified effect unless synchronisation ensures that they occur after all threads have executed all of their iterations or section constructs, and the reduction computation has completed and stored the computed value of that list item. This can most simply be ensured through a barrier synchronisation.

Copy

Feedback

nowait

Example

Copy

Feedback

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <omp.h>

/**
 * @brief Illustrates how to use a nowait clause.
 * @details A parallel region is created, in which one thread executes a single
 * construct while the other one skips it without waiting thanks to the nowait
 * clause.
 **/
int main(int argc, char* argv[])
{
    // Use 4 threads when creating OpenMP parallel regions
    omp_set_num_threads(2);

    // This semaphore is used to sequentialise printfs.
    bool handshake = false;

    // Create the parallel region
    #pragma omp parallel default(none) shared(handshake)
    {
        #pragma omp single nowait
        {
            printf("Thread %d got into the single construct.\n", omp_get_thread_num());
            while(!handshake)
            {

            }
        }

        #pragma omp critical
        {
            if(!handshake)
            {
                printf("Thread %d skipped the single clause, not waiting for the other thread thanks to the nowait clause.\n", omp_get_thread_num());
                handshake = true;
            }
            else
            {
                printf("Thread %d has now completed the single construct.\n", omp_get_thread_num());
            }
        }
    }

    return EXIT_SUCCESS;
}