Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Thread question

Posted on 2004-09-23
13
Medium Priority
?
220 Views
Last Modified: 2010-04-05
I am learning my way through the use of threads and would appreciate any information that anyone can give in regard to the following example.


Example:

Procdeure/Function A
begin
        try
         EnterCriticalSection(Sect);

        {Perform actions here}

       finally
         LeaveCriticalSection(Sect);        
       
       end;
end;

Procdeure/Function B
begin
        try
         EnterCriticalSection(Sect);

        {Perform actions here}

       finally
         LeaveCriticalSection(Sect);        
       
       end;
end;

Question: What is the best way to ensure that While Procedure/Function A is being executed that Procedure/Function B cannot execute and vice-versa.  Both Procedure/Function are in the same unit if that helps and both Procedure/Functions can be called indivdually within the main program.
0
Comment
Question by:gshep
  • 7
  • 3
  • 3
13 Comments
 
LVL 2

Expert Comment

by:__alex
ID: 12132527
You've already done everything (as long as Sect in A is Sect in B)!?
And as I said in your previous Q: This is a good tutorial and it helped me a lot: http://www.pergolesi.demon.co.uk/prog/threads/ToC.html
0
 

Author Comment

by:gshep
ID: 12132623
You mean that While A is processing, nothing can enter B and vice versa?  ie: if i call A and then B is then called before A is finished what will happen?  I thought i would have had to use a "flag" somewhere in A and B so they would be aware when each can continue.
0
 

Author Comment

by:gshep
ID: 12132632
thanks for the link too - i already found it and am slooooowly reading through it as it can be heavy reading in parts.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:gshep
ID: 12132783
Also, the above procedures are NOT in a thread - they are just used by threads and the main VCL thread of course
0
 
LVL 2

Assisted Solution

by:__alex
__alex earned 1000 total points
ID: 12132914
> You mean that While A is processing, nothing can enter B and vice versa?
Yep, that's the only thing what critical sections are good for. They don't protect your variables or something like this. They just ensure that a second thread doesn't get any more CPU time until a first thread called LeaveCriticalSection.
> Also, the above procedures are NOT in a thread
That's not important, threads don't care about the Delphi thread class.
0
 
LVL 17

Expert Comment

by:geobul
ID: 12132966
CriticalSection object is that flag actually. Use one Critical Section object everywhere in all your code where you are accessing one (or several at once) shared resource. In your example above Sect parameter used in all calls of A and B functions must be the same CS Object in order to be sure that if one of these functions is being executed at a specified moment all other calls will be waiting it to finish.

If you are accessing one resource via CS object somewhere that means you must access that resource via the same CS object everywhere in your code. Remember that.

Regards, Geo
0
 
LVL 2

Expert Comment

by:__alex
ID: 12133012
You can try to get a deadlock:
Call EnterCriticalSection in the Execute method of a thread but don't call LeaveCriticalSection. Now call EnterCriticalSection in a button click event. Surprise, surprise, your application is dead.
0
 
LVL 17

Expert Comment

by:geobul
ID: 12133017
For instance, if you have another function C which doesn't use CS object 'Sect' but does something with the resource protected by Sect then function C can be executed while one of A or B is being executed.
0
 

Author Comment

by:gshep
ID: 12133066
ok, just to make sure i have got it right....

In the example above (which are not threads but part of the main VCL thread) if i call A then no other thread, including the VCL can enter B until A is finished and "Leaves" the section.  And as long as all Enter/Leave sections use the same criticalsection "flag" (in this case Sect) then all code in that unit that have the same enter/leave flag around them must wait until any other code block has exited via a leave section.

Also, if A is processing and B is called (from within the VCL thread) i assume that i do not need to worry about recalling it as the call to B will just "wait" till A is finished then it will get to do its work. [ie: if the VCL calls A then B then B - A will begin to process, first call to B will wait as will second call (without me doing anything else) then when A is done, the first B will process and when done, the second B]

Have i got it right so far?

 
0
 

Author Comment

by:gshep
ID: 12133084
geobul - so i make sure Sect is global to everyone calling these routines and use the same enter/leave throughout the entire program.
0
 

Author Comment

by:gshep
ID: 12133090
raised points as i really appreciate the help in learning this stuff.
0
 
LVL 17

Accepted Solution

by:
geobul earned 1000 total points
ID: 12135297
Sorry, I was busy for while ...

Yes, exactly. Everything is as you describe it. That is the purpose of critical sections.

>so i make sure Sect is global to everyone calling these routines and use the same enter/leave throughout the entire program.

Well, you might do so. You might declare Sect in the same unit where A and B are defined. If Sect is used by A and B routines only then you could also define Sect in their unit this way:

unit ...

interface
uses Windows;
routine A;
routine B;
...

implementation

var Sect: TRTLCriticalSection;

routine A;
...

routine B;
...

initialization
  InitializeCriticalSection(Sect);

finalization
  DeleteCriticalSection(Sect);

end;
 

Regards, Geo
0
 

Author Comment

by:gshep
ID: 12143723
Thank you - to both of you - you have helped me a lot in understanding a bit more about threads.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question