Solved

Thread question

Posted on 2004-09-23
13
212 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

707 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

11 Experts available now in Live!

Get 1:1 Help Now