EnterCriticalSection - why ?

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 ?




asiAsked:
Who is Participating?
 
FrodoBegginsConnect With a Mentor Commented:
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
 
Andrey_KulikCommented:
your samples means the same

don't worry be happy :-)

Andrey
0
 
Colin_DawsonCommented:
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
asiAuthor Commented:
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
 
asiAuthor Commented:
Does the EnterCriticalSection(CriticalSec);

can fail ?
0
 
Andrey_KulikCommented:
no
0
 
Tal_avidorCommented:
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
 
asiAuthor Commented:
Tal Vidor : why ?
0
 
FrodoBegginsCommented:
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
 
FrodoBegginsCommented:
Tal Vidor: Why creating kernel object (which may stay alive even if we quit the app!) if we can avoid it?
0
 
FrodoBegginsCommented:
2. asi: see if you call InitializeCriticalSection(CriticalSec) before entering it.
0
 
asiAuthor Commented:
Frobo : yes its on the first line on Tform.FormCreate
0
 
FrodoBegginsCommented:
Me - Frodo :)
Asi, do you use CriticalSection kernel object or TCriticalSection VCL object?
0
 
asiAuthor Commented:


I have
CriticalSec: TRTLCriticalSection;

how am i define CriticalSection kernel  ?
how am i define TCriticalSection VCL object ?
0
 
FrodoBegginsCommented:
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
 
MadshiCommented:
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
 
MadshiCommented:
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
 
FrodoBegginsCommented:
 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
 
MadshiCommented:
>> 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
 
MoondancerCommented:
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
 
NetminderCommented:
Admin notified of User neglect. Force-accepted by
Netminder
Community Support Moderator
Experts Exchange
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.