Code: Select all
int main()
{
// only the main program thread exists here
#pragma omp parallel num_threads(2)
{
if (omp_get_thread_num() == 0)
{
// this block is guaranteed to be executed by the main program thread
}
}
}
Code: Select all
int main()
{
// only the main program thread exists here
#pragma omp parallel num_threads(2)
{
if (omp_get_thread_num() == 1)
{
#pragma omp parallel num_theads(2)
{
if (omp_get_thread_num() == 0)
{
// this block is NOT executed by the program main thread
}
}
}
}
}
Live-Demo: https://godbolt.org/z/5K3ja8fY1
Meine Frage ist, ob es eine Möglichkeit gibt, den Hauptprogramm-Thread innerhalb einer OpenMP-Parallelregion immer zu erkennen funktioniert.
(Ich könnte für diesen Zweck möglicherweise std::this_thread::get_id() verwenden, wie in der Demo gezeigt; ich bin mir jedoch nicht sicher, ob die Portabilität zusammen mit OpenMP gewährleistet ist.)
Mobile version