OpenMP Parallel Sections
suggest changeThis example shows how to execute chunks of code in parallel
std::cout << "begin ";
// Start of parallel sections
#pragma omp parallel sections
{
// Execute these sections in parallel
#pragma omp section
{
... do something ...
std::cout << "hello ";
}
#pragma omp section
{
... do something ...
std::cout << "world ";
}
#pragma omp section
{
... do something ...
std::cout << "forever ";
}
}
// end of parallel sections
std::cout << "end";
Output
- begin hello world forever end
- begin world hello forever end
- begin hello forever world end
- begin forever hello world end
begin is printed before the parallel region and end after it. The three words can appear in any order, and their characters can also interleave, because the stream insertions are not synchronized.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents