Solved

Thread question

Posted on 2004-09-23
13
215 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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

821 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