Solved

Thread question

Posted on 2004-09-23
13
214 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

832 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