[Omp] Critical Section deadlock
Andreas Gajda
Gajda at zhr.tu-dresden.de
Wed Aug 2 09:23:10 PDT 2006
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
#####################################################################
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.openmp.org/pipermail/omp/attachments/20060802/15035765/attachment.html
More information about the Omp
mailing list