Table of Contents
OpenMP Critical sections
Critical sections in OpenMP protect an arbitrary sequence of shared updates with an implicit mutex: only one thread at a time executes the enclosed block, and threads that arrive while another is inside are blocked until it exits. Unlike `atomic`, which is limited to a single read-modify-write operation, critical can protect complex sequences of statements that POSIX would protect with explicit pthread_mutex_lock / pthread_mutex_unlock calls.
#pragma omp parallel for for (int i = 0; i < N; i++) { int val = expensive_compute(i); #pragma omp critical { if (val > best) { best = val; best_idx = i; } } }
Critical sections can be named (#pragma omp critical(name)) so that threads only contend with sections sharing the same name. Two differently-named critical sections can be entered simultaneously by different threads. Unnamed sections all share a single implicit lock, so mixing named and unnamed sections in the same program can cause unexpected serialisation.
