Link to home
Start Free TrialLog in
Avatar of xutao
xutao

asked on

When To Call InitializeCriticalSection

In MSDN, it is said:
...
The process is responsible for allocating the memory used by a critical section object, which it can do by declaring a variable of type CRITICAL_SECTION. Before using a critical section, some thread of the process must call the InitializeCriticalSection or InitializeCriticalSectionAndSpinCount function to initialize the object.
...

Does it necessary for each thread which uses the critical section object to call InitializeCriticalSection before calling EnterCriticalSection?
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
just create a global object of jkr's __critical_section and use it, its constructor will take care of setting it up.
Avatar of xutao
xutao

ASKER

In fact, I use ATL's critical section class which does the same thing as jkr's class in my code.
Then the problem is some time when I tried to enter the critical section, an exception is thrown. I'm sure that the memory is enough and the STATUS_INVALID_HANDLE exception should not be raised. But the strange thing is after I using SEH to wrap the EnterCriticalSection call, nothing happened, why?
>>after I using SEH to wrap the EnterCriticalSection call, nothing happened, why?

That's strange - could you post some code?
Please update and finalize this old, open question. Please:

1) Award points ... if you need Moderator assistance to split points, comment here with details please or advise us in Community Support with a zero point question and this question link.
2) Ask us to delete it if it has no value to you or others
3) Ask for a refund so that we can move it to our PAQ at zero points if it did not help you but may help others.

EXPERT INPUT WITH CLOSING RECOMMENDATIONS IS APPRECIATED IF ASKER DOES NOT RESPOND.

Thanks,

** Mindphaser - Community Support Moderator **

P.S.  Click your Member Profile, choose View Question History to go through all your open and locked questions to update them.
Dear xutao

I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. You can always request to keep this question open. But remember, experts can only help you if you provide feedback to their questions.
Unless there is objection or further activity,  I will suggest to

     "PAQ at zero points"

since you never gave more feedback.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
Objection - I pretty much answered the question:

Comment
From: jkr  Date: 04/24/2002 08:18AM PST  
>>Does it necessary for each thread which uses the
>>critical section object to call
>>InitializeCriticalSection before calling
>>EnterCriticalSection?

No, you only have to do that once, not on a per thread basis. This little code snippet might help to automate that:

 


Force accepted

** Mindphaser - Community Support Moderator **