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
Printable View
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
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
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
A try/catch won't help you much on an entirly C based API :). But __try/__except may help you.
Why would you want to do this? :confused:Quote:
Originally Posted by avi123
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.
Maybe you can do something like this:Quote:
Originally Posted by avi123
- petterCode:__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...
}
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
Use code tags when posting code!
http://msdn.microsoft.com/library/de...calsection.asp
MSDN is your freind ^_^ (sometimes :\)