Solved

When To Call InitializeCriticalSection

Posted on 2002-04-24
8
1,556 Views
Last Modified: 2010-05-18
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?
0
Comment
Question by:xutao
8 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 50 total points
ID: 6966036
>>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:

class __CRITICAL_SECTION {

public:

     __CRITICAL_SECTION();
     ~__CRITICAL_SECTION();

     void Enter ();
     void Leave ();

private:

     CRITICAL_SECTION m_cs;
};

__CRITICAL_SECTION::__CRITICAL_SECTION () {

     InitializeCriticalSection ( &m_cs);
}

__CRITICAL_SECTION::~__CRITICAL_SECTION () {

     DeleteCriticalSection ( &m_cs);
}

void
__CRITICAL_SECTION::Enter () {

     EnterCriticalSection ( &m_cs);
}

void
__CRITICAL_SECTION::Leave () {

     LeaveCriticalSection ( &m_cs);
}

0
 
LVL 10

Expert Comment

by:makerp
ID: 6968149
just create a global object of jkr's __critical_section and use it, its constructor will take care of setting it up.
0
 
LVL 1

Author Comment

by:xutao
ID: 6968977
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?
0
 
LVL 86

Expert Comment

by:jkr
ID: 6968993
>>after I using SEH to wrap the EnterCriticalSection call, nothing happened, why?

That's strange - could you post some code?
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 6

Expert Comment

by:Mindphaser
ID: 7036255
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.
0
 
LVL 11

Expert Comment

by:griessh
ID: 7122151
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
0
 
LVL 86

Expert Comment

by:jkr
ID: 7122248
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:

 


0
 
LVL 6

Expert Comment

by:Mindphaser
ID: 7138078
Force accepted

** Mindphaser - Community Support Moderator **
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

759 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

19 Experts available now in Live!

Get 1:1 Help Now