Solved

When To Call InitializeCriticalSection

Posted on 2002-04-24
8
1,566 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

 
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
 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

808 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