# Binary Semaphore runs on task initialising

**URL:** <https://forums.freertos.org/t/binary-semaphore-runs-on-task-initialising/1532>\
**Category:** Kernel\
**Created:** [March 31, 2009, 4:31pm UTC](https://forums.freertos.org/t/binary-semaphore-runs-on-task-initialising/1532 "2009-03-31T16:31:51Z")\
**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:** [March 31, 2009, 4:31pm UTC](https://forums.freertos.org/t/binary-semaphore-runs-on-task-initialising/1532/1 "2009-03-31T16:31:51Z")

</div>

**[phil4321](https://sourceforge.net/u/phil4321/)** wrote on [Tuesday, March 31, 2009](https://sourceforge.net/p/freertos/discussion/382005/thread/453241b5/#5f8a):

Hi,

I have created a binary semaphore in my module initialization code:

vSemaphoreCreateBinary( xBinarySemaphoreRxMsg );

It initializes ok,&nbsp; I then create a new task to run:

xTaskCreate( vRX\_MsgHandlerTask, "RX", MSG\_TASK\_STACK\_SIZE, NULL, xTask\_Priority, NULL );

All ok, here is the basic task code:

void&nbsp; vRX\_MsgHandlerTask( void \*pvParameters )  
{  
&nbsp;&nbsp;&nbsp; unsigned portSHORT i;  
&nbsp;&nbsp;&nbsp; //unsigned portSHORT usFrameIndex;

&nbsp;&nbsp;&nbsp; ulMsgCounter = 0;  
&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp; for( ;; )  
&nbsp;&nbsp;&nbsp; {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //blocking for RX event to happen  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; xSemaphoreTake( xBinarySemaphoreRxMsg , portMAX\_DELAY );

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TRACE\_sprintf("Processing the Data\r\n");  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }  
}

Now when the vRX\_MsgHandlerTask starts for the first time the TRACE\_sprintf("Processing the Data\r\n"); is called and outputs to the console. I am pretty sure there has been no call to xSemaphoreGive( xBinarySemaphoreRxMsg&nbsp; ); it is as if the binary semaphore thinks it can take the semaphore, the code runs as expected after this has happened.

Is this a school boy error, hope you can help.

Phil

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [March 31, 2009, 5:04pm UTC](https://forums.freertos.org/t/binary-semaphore-runs-on-task-initialising/1532/2 "2009-03-31T17:04:21Z")

</div>

**[adarkar9](https://sourceforge.net/u/adarkar9/)** wrote on [Tuesday, March 31, 2009](https://sourceforge.net/p/freertos/discussion/382005/thread/453241b5/#5f8a/3c4f):

The semaphore starts out available to be taken (check out the definition of “vSemaphoreCreateBinary” in “semphr.h”).&nbsp; You must take it first outside of your task’s loop if you don’t want it to be available when your loop first runs.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [March 31, 2009, 5:13pm UTC](https://forums.freertos.org/t/binary-semaphore-runs-on-task-initialising/1532/3 "2009-03-31T17:13:22Z")

</div>

**[phil4321](https://sourceforge.net/u/phil4321/)** wrote on [Tuesday, March 31, 2009](https://sourceforge.net/p/freertos/discussion/382005/thread/453241b5/#5f8a/8e71):

Thanks for the quick response that fixed the problem. 🙂

Phil
