主题:[讨论]一个非常困难的问题,求救于各位大侠
cvf6.5环境下的多进程FORTRAN程序:
program hello
use dfwin
interface
subroutine thread_hello1()
end subroutine
end interface
interface
subroutine thread_hello2()
end subroutine
end interface
integer(4) tickte,Ssection,ThreadID1,ThreadID2,hThread1,hThread2,waitresult
common /tick/hThread1,hThread2,Ssection,tickte
tickte=100000
call InitializeCriticalSection(loc(Ssection)) !初始CriticalSection
write(6,*) "head of the main program"
!创建线程于thread_hello1,thread_hello2
hThread1=CreateThread(NULL_SECURITY_ATTRIBUTES, 100, loc(thread_hello1),0,0, loc(ThreadID1))
hThread2=CreateThread(NULL_SECURITY_ATTRIBUTES, 100, loc(thread_hello2), 0,CREATE_SUSPENDED, loc(ThreadID2))
call entercriticalsection(loc(Ssection)) !dubug时tickte被减1
write(6,*) 'got the critical section,leave now' !dubug时tickte又被减1
call leavecriticalsection(loc(Ssetion))
waitresult=waitforsingleobject(hTHREAD1,-1) ! "-1"=无穷时间等待
waitresult=waitforsingleobject(hTHREAD2,-1) ! "-1"=无穷时间等待
write(6,*) "end of the main program",tickte
pause
end program hello
!###################################################################
!被调用的线程子程序,将大于1的tickte减至1后结束
!
subroutine thread_hello1()
use dfwin
integer(4) hThread1,hThread2,Ssection,tickte
common /tick/hThread1,hThread2,Ssection,tickte
do while(tickte>=1)
call entercriticalsection(loc(Ssection))
if(tickte>1) then
tickte=tickte-1
end if
call leavecriticalsection(loc(Ssection))
end do
call exitthread(hthread1)
end subroutine thread_hello1
!###################################################################
!被调用的线程子程序,将大于1的tickte减至1后结束,与thread_hello1相同
!
subroutine thread_hello2()
use dfwin
integer(4) hThread1,hThread2,Ssection,tickte
common /tick/hThread1,hThread2,Ssection,tickte
do while(tickte>=1)
call entercriticalsection(loc(Ssection))
if(tickte>1) then
tickte=tickte-1
end if
call leavecriticalsection(loc(Ssection))
end do
call exitthread(hthread2)
end subroutine thread_hello2
=========================================================================
=========================================================================
请问各位大侠,为什么的我最后得到的tickte结果不是1,而是-1
尤其不懂的地方是主进程在调用 call entercriticalsection(loc(Ssection))时,tickte又被减1 (在debug时看到的)
非常感谢!!!
program hello
use dfwin
interface
subroutine thread_hello1()
end subroutine
end interface
interface
subroutine thread_hello2()
end subroutine
end interface
integer(4) tickte,Ssection,ThreadID1,ThreadID2,hThread1,hThread2,waitresult
common /tick/hThread1,hThread2,Ssection,tickte
tickte=100000
call InitializeCriticalSection(loc(Ssection)) !初始CriticalSection
write(6,*) "head of the main program"
!创建线程于thread_hello1,thread_hello2
hThread1=CreateThread(NULL_SECURITY_ATTRIBUTES, 100, loc(thread_hello1),0,0, loc(ThreadID1))
hThread2=CreateThread(NULL_SECURITY_ATTRIBUTES, 100, loc(thread_hello2), 0,CREATE_SUSPENDED, loc(ThreadID2))
call entercriticalsection(loc(Ssection)) !dubug时tickte被减1
write(6,*) 'got the critical section,leave now' !dubug时tickte又被减1
call leavecriticalsection(loc(Ssetion))
waitresult=waitforsingleobject(hTHREAD1,-1) ! "-1"=无穷时间等待
waitresult=waitforsingleobject(hTHREAD2,-1) ! "-1"=无穷时间等待
write(6,*) "end of the main program",tickte
pause
end program hello
!###################################################################
!被调用的线程子程序,将大于1的tickte减至1后结束
!
subroutine thread_hello1()
use dfwin
integer(4) hThread1,hThread2,Ssection,tickte
common /tick/hThread1,hThread2,Ssection,tickte
do while(tickte>=1)
call entercriticalsection(loc(Ssection))
if(tickte>1) then
tickte=tickte-1
end if
call leavecriticalsection(loc(Ssection))
end do
call exitthread(hthread1)
end subroutine thread_hello1
!###################################################################
!被调用的线程子程序,将大于1的tickte减至1后结束,与thread_hello1相同
!
subroutine thread_hello2()
use dfwin
integer(4) hThread1,hThread2,Ssection,tickte
common /tick/hThread1,hThread2,Ssection,tickte
do while(tickte>=1)
call entercriticalsection(loc(Ssection))
if(tickte>1) then
tickte=tickte-1
end if
call leavecriticalsection(loc(Ssection))
end do
call exitthread(hthread2)
end subroutine thread_hello2
=========================================================================
=========================================================================
请问各位大侠,为什么的我最后得到的tickte结果不是1,而是-1
尤其不懂的地方是主进程在调用 call entercriticalsection(loc(Ssection))时,tickte又被减1 (在debug时看到的)
非常感谢!!!