# FreeRTOS + POSIX race condition in pthread\_cond\_timedwait()

**URL:** <https://forums.freertos.org/t/freertos-posix-race-condition-in-pthread-cond-timedwait/8265>\
**Category:** Kernel\
**Created:** [November 14, 2019, 10:11pm UTC](https://forums.freertos.org/t/freertos-posix-race-condition-in-pthread-cond-timedwait/8265 "2019-11-14T22:11:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [November 14, 2019, 10:11pm UTC](https://forums.freertos.org/t/freertos-posix-race-condition-in-pthread-cond-timedwait/8265/1 "2019-11-14T22:11:55Z")

</div>

**[roku-don](https://sourceforge.net/u/roku-don/)** wrote on [Thursday, November 14, 2019](https://sourceforge.net/p/freertos/discussion/382005/thread/ad435bcff6/#fdbe):

Hello forum,

On 8/14/2019 I downloaded FreeRTOS+POSIX and from reading the code in FREERTOS\_POSIX\_pthead\_cond.c I believed I had found a race condition in pthread\_cond\_timedwait().

On that day I filed the following bug with detailed explanation including code-snipptes:  
[https://sourceforge.net/p/freertos/bugs/197/](https://sourceforge.net/p/freertos/bugs/197/)

Today I hit the race condition, confirming it exists.

No one has yet been assigned owner for this bug.

I am REQUESTING ASSISTANCE OR AN UPDATE on this bug.

Thank you,  
-Don Woodward

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [November 15, 2019, 2:38am UTC](https://forums.freertos.org/t/freertos-posix-race-condition-in-pthread-cond-timedwait/8265/2 "2019-11-15T02:38:07Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Friday, November 15, 2019](https://sourceforge.net/p/freertos/discussion/382005/thread/ad435bcff6/#fdbe/c3f3):

I Don - we are looking at this now and will provide an update shortly.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [November 15, 2019, 6:06pm UTC](https://forums.freertos.org/t/freertos-posix-race-condition-in-pthread-cond-timedwait/8265/3 "2019-11-15T18:06:56Z")

</div>

**[yuhzheng](https://sourceforge.net/u/yuhzheng/)** wrote on [Friday, November 15, 2019](https://sourceforge.net/p/freertos/discussion/382005/thread/ad435bcff6/#6499):

Hi Don,

I could confirm the bug you reported on count underflow was a solid bug, and I do apologize for the delayed response to the forum post.

The bug had been fixed with the project re-uploaded to [freertos.org](http://freertos.org) lab. We did drop the communication on this change, which was also on us.

The most recent FreeRTOS+POSIX lab project could be downloaded here.  
[https://www.freertos.org/FreeRTOS-Labs/RTOS\_labs\_download.html](https://www.freertos.org/FreeRTOS-Labs/RTOS_labs_download.html)  
POSIX is located under `./FreeRTOS-Plus/Demo/FreeRTOS_Plus_POSIX_with_actor_Windows_Simulator/lib/FreeRTOS-Plus-POSIX`.

Please note that:

- The folder strutcture has been changed.
- This version (zip name 190820-\*) of POSIX depends on `atomic.h`. (we made some performance improvements.)

Also, to your specific concern about `pthread_cond_timedwait()`:

- This function does test- test-and-set logic on counter decrement. This addresses the underflow issue you’ve reported. Please check `prvTestAndDecrement()`.
- `pthread_cond_timedwait()` mutex release and count semaphore take in 190820-\* is not “atomic” in our implementation. Please kindly note this is different from POSIX specification.  
[http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread\_cond\_timedwait.html](http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_cond_timedwait.html).  
quote "These functions atomically release mutex and cause the calling thread to block on the condition variable cond; atomically here means “atomically with respect to access by another thread to the mutex and then the condition variable”

We are working on a better versioning scheme of freertos lab code. And hopefully next time the source control could state for itself for which version a user is consuming.

Regards,
