Solved

When To Call InitializeCriticalSection

Posted on 2002-04-24
8
1,562 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

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. …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

776 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