Solved

EnterCriticalSection - why ?

Posted on 2001-06-18
21
890 Views
Last Modified: 2007-11-27
Hello,


what is the right way and why ?
"
  try
    EnterCriticalSection(CriticalSec);
....
  finally
    LeaveCriticalSection(CriticalSec);
  end;
"

OR

"
  EnterCriticalSection(CriticalSec);
  try
    ....
  finally
    LeaveCriticalSection(CriticalSec);
  end;
"?


another question :

Is it important that the  CriticalSec.LockCount
will be 0
sometimes its get to very big number -22343234, can i get any conclution from it ?




0
Comment
Question by:asi
  • 7
  • 5
  • 3
  • +5
21 Comments
 
LVL 2

Expert Comment

by:Andrey_Kulik
ID: 6202352
your samples means the same

don't worry be happy :-)

Andrey
0
 
LVL 4

Expert Comment

by:Colin_Dawson
ID: 6202367
To answer your question you must ask another....

 EnterCriticalSection(CriticalSec);

If this line of code fails, do I want to call

   LeaveCriticalSection(CriticalSec);

?

That's basically what you're asking us.  Personally I think that if EnterCriticalSection you don't really want to call the LeaveCriticalSection because you didn't actually enter it!

Colin Dawson
0
 

Author Comment

by:asi
ID: 6202394
does b4 geting to critical section the
CriticalSec.LockCount should be 0

if not (somtimes CriticalSec.LockCount is very big), what should i understand from it ?


10x
Asi
0
 

Author Comment

by:asi
ID: 6202400
Does the EnterCriticalSection(CriticalSec);

can fail ?
0
 
LVL 2

Expert Comment

by:Andrey_Kulik
ID: 6202421
no
0
 
LVL 1

Expert Comment

by:Tal_avidor
ID: 6202632
Hi asi

do not do this

 try
   EnterCriticalSection(CriticalSec);
....
 finally
   LeaveCriticalSection(CriticalSec);
 end;


Because if EnterCriticalSection had an error it will not start then u do not need to leave it.

b.w try using Mutexs instead of using CriticalSection !!!!
0
 

Author Comment

by:asi
ID: 6202712
Tal Vidor : why ?
0
 
LVL 2

Expert Comment

by:FrodoBeggins
ID: 6203042
1. "EnterCriticalSection" before the "try". But I dont think it really matters - if you have not enough resources to enter in a CS, then u r in very big trouble.

2. I think it should be 0

Tal Vidor: I think if u can do it with critical sections, you don't need mutexes. U need mutexes only if you need to synchronize data access across multiple processes.
0
 
LVL 2

Expert Comment

by:FrodoBeggins
ID: 6203054
Tal Vidor: Why creating kernel object (which may stay alive even if we quit the app!) if we can avoid it?
0
 
LVL 2

Expert Comment

by:FrodoBeggins
ID: 6203071
2. asi: see if you call InitializeCriticalSection(CriticalSec) before entering it.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:asi
ID: 6203095
Frobo : yes its on the first line on Tform.FormCreate
0
 
LVL 2

Expert Comment

by:FrodoBeggins
ID: 6203180
Me - Frodo :)
Asi, do you use CriticalSection kernel object or TCriticalSection VCL object?
0
 

Author Comment

by:asi
ID: 6203197


I have
CriticalSec: TRTLCriticalSection;

how am i define CriticalSection kernel  ?
how am i define TCriticalSection VCL object ?
0
 
LVL 2

Expert Comment

by:FrodoBeggins
ID: 6205179
Yh... TRTLCriticalSection is some wrapper I never used. There is TCriticalSection Delphi Object (decedent from TSynchroObject, decedent from TObject) which you may use too. But it is wrapper again. The easiest for me is Windows' critical section (it is not kernel object, I was wrong yesterday). It is directly calling widows API functions
Just define an CRITICAL_SECTION (global!!!) object and use:
InitializeCriticalSection(CriticalSec); // use it in your main thread
EnterCriticalSection(CriticalSec);
LeaveCriticalSection(CriticalSec);
DeleteCriticalSection(CriticalSec); // when you don't need it any more
There is a TryEnterCriticalSection(CriticalSec); function, but it is implemented only under the NT platform. If you use it in your program, you'll not be able to start it under 9x.
0
 
LVL 20

Expert Comment

by:Madshi
ID: 6205259
Hi Frodo,

I agree with most of your posts, but not with all:

>> Why creating kernel object (which may stay alive even if we quit the app!) if we can avoid it?

Windows closes kernel objects for us when our program gets terminated, so mutexes do not stay alive. However, mutexes have a much larger performance penalty. So you should really go with critical sections, if they're good enough...

>> TRTLCriticalSection is some wrapper I never used. [...] The easiest for me is Windows' critical section [...]. Just define an CRITICAL_SECTION (global!!!) object

I don't have CRITICAL_SECTION declared anywhere. But instead of you look in "WinBase.h", you'll find this line:

typedef RTL_CRITICAL_SECTION CRITICAL_SECTION;

So in fact TRTLCriticalSection is no wrapper, but it is exactly CRITICAL_SECTION. At least as far as I understand it.

Hi Asi,

I agree with the others. The Enter should be before the try. And the lock count should be 0. How do you test the lock count? The IDE debugger sometimes gives you wrong values. Use a MessageBox call instead to make sure the lock count really is wrong...

Regards, Madshi.
0
 
LVL 2

Accepted Solution

by:
FrodoBeggins earned 50 total points
ID: 6205302
Hi, Madshi
I'm a newbee arround here but I see u r a star. I'm glad you saw me :)
For the questions:
>> Windows closes kernel objects for us when our program gets terminated
This is NOT true. A kernel object is destroyed after it's usage count becomes 0 (all the hadles are closed). So if you don't close the handle to it it will remain in the memory (anothe process my need using it).

>> typedef RTL_CRITICAL_SECTION CRITICAL_SECTION;
Sorry, I'm not using C. In delphi's windows.pas you'll find
_RTL_CRITICAL_SECTION = record
  DebugInfo: PRTLCriticalSectionDebug;
  LockCount: Longint;
  RecursionCount: Longint;
  OwningThread: THandle;
  LockSemaphore: THandle;
  Reserved: DWORD;
end;
{$EXTERNALSYM _RTL_CRITICAL_SECTION}
TRTLCriticalSection = _RTL_CRITICAL_SECTION;

As I mentioned, I'm a newbee. I may be wrong. But if I'm wrong, I'll be glad someone to show me the right path.
0
 
LVL 20

Expert Comment

by:Madshi
ID: 6205353
Hi Frodo...

>> I'm a newbee arround here

Maybe, but most of your comments I saw seemed quite qualified to me...   (-:

>> This is NOT true. A kernel object is destroyed after it's usage count becomes 0 (all the hadles are closed). So if you don't close the handle to it it will remain in the memory (anothe process my need using it).

Okay, yes, you're right. I basically meant the same thing, I just didn't express it well. When a win32 process is terminated, Windows does something like:

for i1 := 0 to OpenHandles.ItemCount - 1 do
  CloseHandle(OpenHandles[i1].Handle);

So if no other process still holds a handle to our mutex, the mutex is closed when our process terminates. Why should another process hold a handle to our mutex? I see no reason for that, except the mutex is really used to synchronize multiple processes. But that's not the case in this question. So in this question where a Mutex would only be used in one process, we can almost surely say, that the Mutex will be closed by Windows, when our process terminates. So saying "don't use a Mutex, cause it might stay open even after our process terminated" does not seem very logical to me in this very case.

>> Sorry, I'm not using C.

I'm not using C, either. I just wanted to say, that IMO TRTLCriticalSection is exactly the same as CRITICAL_SECTION. Ehm... btw, where did you find a "CRITICAL_SECTION" definition in Delphi? At least I can't find it in "Windows.pas".

Regards, Madshi.
0
 
LVL 2

Expert Comment

by:FrodoBeggins
ID: 6205431
 Well, Madshi, I'm not quite sure about closing the handles. I'll check it, but I think it remains open. It is easy to check it.
  In Delphi 5 Enterprise, look at line 2048 .. 2076 in windows.pas for critical sections.
  That's getting a little out-of-topic. But without any source it is dificult to me (us) to find the bug.
  To sumarize: (1) enter before try, (2) you need 0. The number you wrote looks quite random (FFFF FFFF FEAB 11BE) - the smaller DWord, at least. Please, asi, help us to help you.
0
 
LVL 20

Expert Comment

by:Madshi
ID: 6205472
>> In Delphi 5 Enterprise, look at line 2048 .. 2076 in windows.pas for critical sections.

I know that, of course. Okay, in short, you said "Just define an CRITICAL_SECTION (global!!!) object", but I can't compile the following:

uses Windows;

var cs : CRITICAL_SECTION;  // undefined identifier

Do you see now what I mean? There *IS* no "CRITICAL_SECTION" in Delphi, there only is "TRTLCriticalSection", which however is exactly the same. TRTLCriticalSection is NOT a wrapper (you claimed that)...

Sorry to Asi, in case this is really out of topic...

P.S: To Frodo: If you want to have/continue out-of-topic discussion, you can mail me at frodo@madshi.net...  :-)
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6844652
Question(s) below appears to have been abandoned. ADMINISTRATION HAS BEEN ADVISED AND WILL BE CONTACTING YOU SHORTLY.  There are a total of 57 open questions for you, any not shown here have ZERO comments added, so you can delete them directly.

Your options are:
 
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you. You must tell the participants why you wish to do this, and allow for Expert response.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question. Again, please comment to advise the other participants why you wish to do this.

For special handling needs, please post a zero point question in the link below and include the question QID/link(s) that it regards.
http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
Please click the Help Desk link on the left for Member Guidelines, Member Agreement and the Question/Answer process.  http://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

Please click you Member Profile to view your question history and keep them all current with updates as the collaboration effort continues, to track all your open and locked questions at this site.  If you are an EE Pro user, use the Power Search option to find them.

To view your open questions, please click the following link(s) and keep them all current with updates.
http://www.experts-exchange.com/questions/Q.11624838.html
http://www.experts-exchange.com/questions/Q.11837819.html
http://www.experts-exchange.com/questions/Q.12000718.html
http://www.experts-exchange.com/questions/Q.20015588.html
http://www.experts-exchange.com/questions/Q.20033766.html
http://www.experts-exchange.com/questions/Q.20060547.html
http://www.experts-exchange.com/questions/Q.20086506.html
http://www.experts-exchange.com/questions/Q.20087146.html
http://www.experts-exchange.com/questions/Q.20096878.html
http://www.experts-exchange.com/questions/Q.20099988.html
http://www.experts-exchange.com/questions/Q.20103524.html
http://www.experts-exchange.com/questions/Q.20105868.html
http://www.experts-exchange.com/questions/Q.20124168.html
http://www.experts-exchange.com/questions/Q.20126164.html
http://www.experts-exchange.com/questions/Q.20126258.html
http://www.experts-exchange.com/questions/Q.20127406.html
http://www.experts-exchange.com/questions/Q.11610958.html
http://www.experts-exchange.com/questions/Q.20136024.html
http://www.experts-exchange.com/questions/Q.20137249.html
http://www.experts-exchange.com/questions/Q.20147343.html
http://www.experts-exchange.com/questions/Q.20162739.html
http://www.experts-exchange.com/questions/Q.20163581.html
http://www.experts-exchange.com/questions/Q.20165158.html
http://www.experts-exchange.com/questions/Q.20167166.html
http://www.experts-exchange.com/questions/Q.20177854.html
http://www.experts-exchange.com/questions/Q.20185774.html
http://www.experts-exchange.com/questions/Q.20185782.html
http://www.experts-exchange.com/questions/Q.20211426.html
http://www.experts-exchange.com/questions/Q.20211827.html
http://www.experts-exchange.com/questions/Q.20229407.html
http://www.experts-exchange.com/questions/Q.20231198.html
http://www.experts-exchange.com/questions/Q.20238567.html
http://www.experts-exchange.com/questions/Q.20238568.html
http://www.experts-exchange.com/questions/Q.20238571.html
http://www.experts-exchange.com/questions/Q.20240061.html
http://www.experts-exchange.com/questions/Q.20241871.html
http://www.experts-exchange.com/questions/Q.20242268.html
http://www.experts-exchange.com/questions/Q.20244256.html
http://www.experts-exchange.com/questions/Q.20246106.html
http://www.experts-exchange.com/questions/Q.20250445.html
http://www.experts-exchange.com/questions/Q.20251868.html
http://www.experts-exchange.com/questions/Q.20254886.html
http://www.experts-exchange.com/questions/Q.20254704.html
http://www.experts-exchange.com/questions/Q.20255422.html
http://www.experts-exchange.com/questions/Q.20261740.html
http://www.experts-exchange.com/questions/Q.20262552.html
http://www.experts-exchange.com/questions/Q.20262985.html
http://www.experts-exchange.com/questions/Q.20265275.html
http://www.experts-exchange.com/questions/Q.20270259.html
http://www.experts-exchange.com/questions/Q.20270694.html
http://www.experts-exchange.com/questions/Q.20270687.html
http://www.experts-exchange.com/questions/Q.20271885.html


To view your locked questions, please click the following link(s) and evaluate the proposed answer.
http://www.experts-exchange.com/questions/Q.20171579.html
http://www.experts-exchange.com/questions/Q.20178687.html
http://www.experts-exchange.com/questions/Q.20265222.html
http://www.experts-exchange.com/questions/Q.20265219.html
http://www.experts-exchange.com/questions/Q.20269116.html

PLEASE DO NOT AWARD THE POINTS TO ME.  
 
------------>  EXPERTS:  Please leave any comments regarding your closing recommendations if this item remains inactive another seven (7) days.
 
Thank you everyone.
 
Moondancer
Moderator @ Experts Exchange

P.S.  For any year 2000 questions, special attention is needed to ensure the first correct response is awarded, since they are not in the comment date order, but rather in Member ID order.
0
 
LVL 5

Expert Comment

by:Netminder
ID: 6887767
Admin notified of User neglect. Force-accepted by
Netminder
Community Support Moderator
Experts Exchange
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

746 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

10 Experts available now in Live!

Get 1:1 Help Now