Table of Contents

Sections (OpenMP)

Suppose you have a few independent operations — compress a buffer, encrypt a header, write a log — that share no data and could run simultaneously. A loop doesn't fit because the operations are distinct, not iterations of the same work.

Sections is the OpenMP construct for this case: it distributes a fixed set of independent code blocks across threads at compile time. Each block is wrapped in #pragma omp section.

#pragma omp parallel sections
{
    #pragma omp section
    compress(data);
 
    #pragma omp section
    encrypt(header);
 
    #pragma omp section
    write_log(meta);
}

If there are more section blocks than threads, the excess blocks are queued and picked up as threads become free. If there are fewer blocks than threads, the extra threads sit idle. For a large or dynamic number of tasks, tasks are more flexible; sections is simpler when the count is small and fixed.