Avatar of marb
marb

asked on 

WaitForSingleObject not returning, no timeout

Hello
I have a multithreaded application that uses semaphores to protect critical sections. The application seems to work fine, but I happens after running for several days that the thread is blocked for good. I assume it's the WaitForSingleObject call with timeout value 1000, i.e. 1 second,  that never returns.
I've read about problems using WaitForSingleObject on hyperthreaded computers. Is it possible that no timeout happens and the thread is waiting for the signaled state for an infinite time?
Thanks for your help.
Martin
System ProgrammingC++

Avatar of undefined
Last Comment
marb
Avatar of Deepu Abraham
Deepu Abraham
Flag of United States of America image

Looks like it is tutning the parameter back to infinite.
Can you show how you calling calling WaitForSingleObject?
Best Regards,
DeepuAbrahamK
Avatar of marb
marb

ASKER

This is how I'm calling WaitForSingleObject:

::WaitForSingleObject(m_hSemaphore,1000);

m_hSemaphore is a HANDLE to my semaphore.
ASKER CERTIFIED SOLUTION
Avatar of itsmeandnobodyelse
itsmeandnobodyelse
Flag of Germany image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
SOLUTION
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of marb
marb

ASKER

Thank you for your answers.
The semaphore is checked in a function:
return ( ::WaitForSingleObject(semaphore, timeout) == WAIT_TIMEOUT)

This seems very dangerous to me, because the function is only returning true if a timeout happes, but returns false if signaled OR error. Maybe, this explains why the application is running a long time before hanging. An error gets treated like a signaled state.

I have corrected the return value handling and will run a test over night (it's 15:30 here). I will be back tomorrow with the results of that test. Thank you so far for your help.
Avatar of mxjijo
mxjijo


Good luck /w that . ...
In case if it fails again, you should attach a debuger to the running process and check the call stack of each thread, values of your variables etc. That should give you a better insight.

~j
Avatar of marb
marb

ASKER

My test has been running for 22 hours now with no error. I hope that it's because of the changes regarding the return values. Past tests have shown an error after several days, so I'm still testing.

Please be patient, the test will continue over the weekend and I will reward points on monday.

So long,
Martin

Avatar of marb
marb

ASKER

Hi again,
the test has been running over the weekend without any problems. I guess it was the non-existing error handling that made the app crash. Thank you a lot for your great help.
Martin
C++
C++

C++ is an intermediate-level general-purpose programming language, not to be confused with C or C#. It was developed as a set of extensions to the C programming language to improve type-safety and add support for automatic resource management, object-orientation, generic programming, and exception handling, among other features.

58K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo