Solved

When To Call InitializeCriticalSection

Posted on 2002-04-24
8
1,570 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

733 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