Solved

Thread question

Posted on 2004-09-23
13
213 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
 

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 250 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 250 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

919 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now