[Omp] Critical Section deadlock

Greg Bronevetsky greg at bronevetsky.com
Wed Aug 2 10:06:10 PDT 2006


Section 2.9 (page 87) of the 2.5 spec says:
"A critical region may not be nested (closely or otherwise) inside a 
critical
region with the same name. Note that this restriction is not sufficient 
to prevent
deadlock."

Your first example violates this requirement and is therefore illegal. 
In your compiler of choice it becomes a deadlock but since the OpenMP 
implementation's behavior is undefined in this situation, deadlock is 
not the only thing that may happen. If you want your first example to 
not deadlock, you should use nested locks.

-- 
                             Greg Bronevetsky


Andreas Gajda wrote:
>
> Dear cOMPunity!
>
> I have a question about nested critical sections. Supposed a 
> subroutine sub1 is enclosing a critical section and in this section 
> another subroutine sub2 is called, which contains another critical 
> sections (with the same or a different name).
>
> Is this causing a deadlock or not?
>
> Specification 2.5. says: "A thread waits at the beginning of a 
> critical region until no other thread is executing a critical region 
> with the same name"
>
> Does this mean, the thread must be able to decide, that he already is 
> executing the named critical section and enter the second "part" of 
> the same critical section? On the other hand, if he tests if the 
> critical section is already entered by any thread, he have to wait 
> until "he" is leaving the section. This is the deadlock.
>
> I tested it with two programs and the Intel 9.1 Fortran compiler on a 
> linux platform. Program 1 caused a deadlock and program 2 haven't 
> caused a deadlock. (see below)
>
> The second one is expected, because the critical sections have 
> different names.
>
> Maybe someone can tell me, if my first interpretation of the 
> specification is correct or not. Or if this is already discussed 
> somewhere.
>
> Thank you very much in advance.
>
> Andreas Gajda
>
> #####################################################################
>
> program deadlocktest1
>
> use omp_lib
>
> call omp_set_num_threads(4)
>
> print *,"start test"
>
> call sub1
>
> print *,"test passed. No deadlocks occured!"
>
> stop
>
> contains
>
> subroutine sub1()
>
> integer :: me
>
> !$OMP PARALLEL PRIVATE (me)
>
> me=omp_get_thread_num()
>
> print *,me
>
> !$OMP CRITICAL
>
> print *,"First critical section entered by thread: ", me
>
> call sub2(me)
>
> print *,"First critical section is left by thread: ", me
>
> !$OMP END CRITICAL
>
> !$OMP END PARALLEL
>
> return
>
> end subroutine
>
> subroutine sub2(me)
>
> integer me
>
> !$OMP CRITICAL
>
> print *,"If you can read this, thread ", me," is not deadlocked."
>
> !$OMP END CRITICAL
>
> return
>
> end subroutine
>
> end program
>
> #####################################################################
>
> program deadlocktest2
>
> use omp_lib
>
> call omp_set_num_threads(4)
>
> print *,"start test"
>
> call sub1
>
> print *,"test passed. No deadlocks occured!"
>
> stop
>
> contains
>
> subroutine sub1()
>
> integer :: me
>
> !$OMP PARALLEL PRIVATE (me)
>
> me=omp_get_thread_num()
>
> print *,me
>
> !$OMP CRITICAL (first)
>
> print *,"First critical section entered by thread: ", me
>
> call sub2(me)
>
> print *,"First critical section is left by thread: ", me
>
> !$OMP END CRITICAL (first)
>
> !$OMP END PARALLEL
>
> return
>
> end subroutine
>
> subroutine sub2(me)
>
> integer me
>
> !$OMP CRITICAL (second)
>
> print *,"If you can read this, thread ", me," is not deadlocked."
>
> !$OMP END CRITICAL (second)
>
> return
>
> end subroutine
>
> end program
>
> #####################################################################
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Omp mailing list
> Omp at openmp.org
> http://openmp.org/mailman/listinfo/omp
>   



More information about the Omp mailing list