|
-
May 30th, 2005, 08:48 AM
#1
LeaveCriticalSection
Hi,
I was wondering what happens if I call LeaveCriticalSection API without calling first EnterCriticalSection
Another thing does EnterCriticalSection/LeaveCriticalSection throws an exception (C++ exceptions) if needed
Thanks in advance
avi123
-
May 30th, 2005, 08:54 AM
#2
Re: LeaveCriticalSection
its now ok dont do it, here is what the MSDN says
"If a thread calls LeaveCriticalSection when it does not have ownership of the specified critical section object, an error occurs that may cause another thread using EnterCriticalSection to wait indefinitely"
before you EnterCriticalSection () call TryCriticalSection () to watch if you can gain ownership.
if i helped dont forget to rate :-)
Cheers
-
May 30th, 2005, 09:02 AM
#3
Re: LeaveCriticalSection
Thank Golan, I read it too in the MSDN
the thing is that I want to put in try catch and I don't know if I get to the catch if the critica section was already enter or not I guess then I can use TryEnterCriticalSection
Thanks
Avi123
-
May 30th, 2005, 10:15 AM
#4
Re: LeaveCriticalSection
A try/catch won't help you much on an entirly C based API . But __try/__except may help you.
-
May 30th, 2005, 01:17 PM
#5
Re: LeaveCriticalSection
 Originally Posted by avi123
I was wondering what happens if I call LeaveCriticalSection API without calling first EnterCriticalSection
Why would you want to do this? 
There have to be ways to avoid this situation at all costs.
Exception handling is meant for 'exceptional' (non-expected) situations. It's not meant to compensate for incorrect programming practices. 
Here the problem has been identified.
You should attempt fixing it at it's very roots.
-
May 30th, 2005, 03:30 PM
#6
Re: LeaveCriticalSection
 Originally Posted by avi123
the thing is that I want to put in try catch and I don't know if I get to the catch if the critica section was already enter or not I guess then I can use TryEnterCriticalSection
Maybe you can do something like this:
Code:
__try
{
*/
some code that might throw an exception
*/
// enter a nested try-finally for your synchronized code
__try
{
EnterCriticalSection(...);
*/
more code that might throw an exception
*/
}
__ finally
{
LeaveCriticalSection(...);
}
}
__except(1)
{
// handle exceptions here...
}
- petter
-
May 31st, 2005, 03:20 AM
#7
Re: LeaveCriticalSection
Thank you all
How do I know if EnterCriticalSection failed?
If I want to put it in __try __except what should I except (I mean catch)
what I mean is
__try
{
EnterCriticalSection(m_myCriticSec)
}
__except(/* what should I put in here*/)
{
}
and what can I do if the LeaveCriticalSection fails, Am I stuck I mean the critical section can't be released
Thanks
Avi123
-
May 31st, 2005, 06:41 AM
#8
Re: LeaveCriticalSection
Use code tags when posting code!
-
May 31st, 2005, 01:08 PM
#9
Re: LeaveCriticalSection
In C, you merely shoot yourself in the foot.
In C++, you accidentally create a dozen instances of yourself and shoot them all in the foot. Providing emergency medical care is impossible, because you can't tell which are bitwise copies and which are just pointing at others and saying, "That's me, over there."
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|