prathmesh.kallurkar
prathmesh.kallurkar

Reputation: 5686

Critical region for the threads of current team

I want a piece of code to be critical for the current team of threads and not global critical. How can i achieve this ??

quick-sort(args)
{
    spawn threads #x
    {
       critical-region
       {
            // code
       }
    }
    quick-sort(args)
    quick-sort(args)
}

Here the open-mp critical-region construct will block all threads before accessing the critical region. But i don't have problem with two threads entering the critical region as long as they are not spawned at the same time. I want a solution for openMP.

Upvotes: 0

Views: 177

Answers (1)

Alexey Kukanov
Alexey Kukanov

Reputation: 12774

You cannot do that with #pragma omp critical, but you may use OpenMP locks:

quick-sort(args)
{
    declare an instance of OpenMP lock
    omp_init_lock( the lock instance )
    spawn threads #x
    {
       // critical-region
       omp_set_lock( the lock instance )
       {
            // code
       }
       omp_unset_lock( the lock instance )
    }
    omp_destroy_lock( the lock instance )
    quick-sort(args)
    quick-sort(args)
}

Since each invocation of quick-sort will declare its own lock object, it will give you what you want.

However, from your pseudo-code it seems that you will never have two different thread teams running at the same time, unless there are OpenMP parallel regions in other functions. If the only code that has a parallel region ("spawns threads") is in quick-sort, you would need to have a recursive call to that function from inside the parallel region, which you do not.

Upvotes: 1

Related Questions