# WaitableEvent::wait implementation question

**URL:** <https://forum.juce.com/t/waitableevent-wait-implementation-question/5028>\
**Category:** General JUCE discussion\
**Created:** [March 15, 2010, 10:27pm UTC](https://forum.juce.com/t/waitableevent-wait-implementation-question/5028 "2010-03-15T22:27:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![cpr2323](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/cpr2323/32/18682_2.png) [@cpr2323](https://forum.juce.com/u/cpr2323)\
**Post date:** [March 15, 2010, 10:27pm UTC](https://forum.juce.com/t/waitableevent-wait-implementation-question/5028/1 "2010-03-15T22:27:40Z")

</div>

I was looking thru the code for the posix version of the WaitableEvent::wait function today and I wonder if it has a bug, or I just don’t understand it. If the call to pthread\_cond\_timedwait returns due to what the docs refer to as a ‘spurious wakeup’ (and what the !es-\>triggered loop protects against), then the timeout will be recalculated in full, not taking into account any time already spent waiting, and thus the wait could end up more than timeout supplied, yes? no?

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [March 16, 2010, 8:36am UTC](https://forum.juce.com/t/waitableevent-wait-implementation-question/5028/2 "2010-03-16T08:36:14Z")

</div>

hmm… that’s a good point. It does look like the time calculation should be outside the loop. Thanks, I’ll take a serious look at that.

---

<div class="post-metadata">

**Author:** ![cpr2323](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/cpr2323/32/18682_2.png) [@cpr2323](https://forum.juce.com/u/cpr2323)\
**Post date:** [March 16, 2010, 4:47pm UTC](https://forum.juce.com/t/waitableevent-wait-implementation-question/5028/3 "2010-03-16T16:47:33Z")

</div>

actually, I think the calculation should be inside the loop, but the time-passed since entering the timedwait should be subtracted (and tested) each iteration.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [March 16, 2010, 5:54pm UTC](https://forum.juce.com/t/waitableevent-wait-implementation-question/5028/4 "2010-03-16T17:54:52Z")

</div>

No, you were right the first time. pthread\_cond\_timedwait takes an absolute time, so it should calculate that at the start, and re-use it in the loop. Probably very unlikely to ever cause an issue, but I’ll tidy it up.

---

<div class="post-metadata">

**Author:** ![cpr2323](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/cpr2323/32/18682_2.png) [@cpr2323](https://forum.juce.com/u/cpr2323)\
**Post date:** [March 16, 2010, 6:09pm UTC](https://forum.juce.com/t/waitableevent-wait-implementation-question/5028/5 "2010-03-16T18:09:57Z")

</div>

oh, it’s absolute time, then it’s not really a bug, just a little extra work being done… good to fix it though… 🙂
