Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Deadlock problem

onkelhotte
New Contributor II
404 Views

Hi there,

I have a deadlock problem in my QuickWin Application. My Main Program runs in a do loop:

bCalculationLeft = .false.
do while(bCalculation)
... calling a lot of subroutines and functions ...
end do
bCalculationLeft = .true.

In the menu I have a entry for opening new projects. When the user clicks on that entry, an external subroutine is called and a File opening window will be openend. Meanwhile in the background, the calculation is still running. When the user finally opens a file, the following will be done:

bCalculation = .false.
do while(.not.bCalculationLeft)
call sleepQQ(100)
end do
call loadProject(filename)

Because the loadProject subroutine changes the variables, that are used in the do while(bCalculation) loop, I have to wait until this loop was exited because of Run-Time errors. So I programmend the do while(.not.bCalculationLeft) loop to avoid this.

In 90% this works. In the other 10% the program justs hangs and does not respond anymore.

Is there something I miss or dont do right?

Thanks in advance,
Markus

0 Kudos
3 Replies
DavidWhite
Valued Contributor II
404 Views
Quoting - onkelhotte

Hi there,

I have a deadlock problem in my QuickWin Application. My Main Program runs in a do loop:

bCalculationLeft = .false.
do while(bCalculation)
... calling a lot of subroutines and functions ...
end do
bCalculationLeft = .true.

In the menu I have a entry for opening new projects. When the user clicks on that entry, an external subroutine is called and a File opening window will be openend. Meanwhile in the background, the calculation is still running. When the user finally opens a file, the following will be done:

bCalculation = .false.
do while(.not.bCalculationLeft)
call sleepQQ(100)
end do
call loadProject(filename)

Because the loadProject subroutine changes the variables, that are used in the do while(bCalculation) loop, I have to wait until this loop was exited because of Run-Time errors. So I programmend the do while(.not.bCalculationLeft) loop to avoid this.

In 90% this works. In the other 10% the program justs hangs and does not respond anymore.

Is there something I miss or dont do right?

Thanks in advance,
Markus

Are there 2process threads running?

If not, then once you enter the loop with the Sleep command, then the conditionin the loop can never be changed, and so the loop cannot ever exit.

David
0 Kudos
onkelhotte
New Contributor II
404 Views
Quoting - David White
Are there 2process threads running?

If not, then once you enter the loop with the Sleep command, then the conditionin the loop can never be changed, and so the loop cannot ever exit.

David

I havent created any process threads.

I just use this:

ret=InsertMenuQQ(1,1,$MenuEnabled,'Open Project'C,OpenProjectDialog)

and in the subroutine OpenProjectDialog I have the do while(.not.bCalculationLeft) loop. AFAIK the subroutine OpenProjectDialog is being started as a sperate thread. Because the calculation is still running in the background I think that it is that way. Or is it just because the open dialog is a modeless dialog?

Hm, I just checked with Spy++. The main window has the same thread as the open file dialog...

So, I have to call the subroutine OpenProjectDialog as a seperate thread to archive my goal? How can I do this? Do I have to use CreateThread and ExtThread for this or is there another way?


Markus
0 Kudos
Jugoslav_Dujic
Valued Contributor II
404 Views
Quoting - onkelhotte

I havent created any process threads.

I just use this:

ret=InsertMenuQQ(1,1,$MenuEnabled,'Open Project'C,OpenProjectDialog)

and in the subroutine OpenProjectDialog I have the do while(.not.bCalculationLeft) loop. AFAIK the subroutine OpenProjectDialog is being started as a sperate thread. Because the calculation is still running in the background I think that it is that way. Or is it just because the open dialog is a modeless dialog?

Hm, I just checked with Spy++. The main window has the same thread as the open file dialog...

So, I have to call the subroutine OpenProjectDialog as a seperate thread to archive my goal? How can I do this? Do I have to use CreateThread and ExtThread for this or is there another way?


Markus

You haven't created any threads, but QuickWin has. The "primary" thread runs the message loop, i.e. all the windows, menus, and everything from the callbacks. The "secondary" thread runs the PROGRAM; generally, it is not intended for UI windows, but you may execute a modal dialog from it (because it rides on its own message loop) but not a modeless one.

Now, I admit I didn't exactly understand who waits for whom and what causes the deadlock... however...

You can use global logical variables if you can ensure no deadlock, but you should generally not rely on SLEEPs "for long enough time" (except that sleep(0) yields control to another thread). For more reliable synchronization, you can use CreateEvent/RaiseEvent/WaitForSingleObject APIs. (Note, however, that WaitForSingleObject will halt the thread, and it's unpleasant for the GUI/primary thread).
0 Kudos
Reply