#  vSemaphoreCreateBinary

**URL:** <https://forums.freertos.org/t/vsemaphorecreatebinary/3517>\
**Category:** Kernel\
**Created:** [November 13, 2013, 12:50pm UTC](https://forums.freertos.org/t/vsemaphorecreatebinary/3517 "2013-11-13T12:50:36Z")\
**Posts on this page:** 7\
**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 13, 2013, 12:50pm UTC](https://forums.freertos.org/t/vsemaphorecreatebinary/3517/1 "2013-11-13T12:50:36Z")

</div>

**anonymous** wrote on [Wednesday, November 13, 2013](https://sourceforge.net/p/freertos/discussion/382005/thread/9baa531d/#ff72):

I have two Binary semaphores its not working simultaneously  
here if i comment vReadtask vCriticalTask will execute and if comment vCriticalTask , vReadtask will execute i cant use both simultaneously. What could be the reason? please help

vSemaphoreCreateBinary( xBinarySemaphore );  
vSemaphoreCreateBinary( xReadSemaphore );

and two tasks

xTaskCreate( vReadTask, “Handler”, MEDIUM\_STACK\_SIZE, NULL, 11, &xReadHandler);  
xTaskCreate(vCriticalTask,“Critical”, MEDIUM\_STACK\_SIZE,NULL,12,&xCriticalHandle);

void vReadTask( void \*pvParameters )  
{  
for( ;; )  
{  
xSemaphoreTake( xReadSemaphore, portMAX\_DELAY );

```
     //read code
}

```

}

void vCriticalTask( void \*pvParameters )  
{  
for( ;; )  
{  
xSemaphoreTake( xBinarySemaphore, portMAX\_DELAY );  
//critical code  
}  
}

---

<div class="post-metadata">

**Author:** ![richard-damon](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/richard-damon/32/38_2.png) [@richard-damon](https://forums.freertos.org/u/richard-damon)\
**Post date:** [November 13, 2013, 1:35pm UTC](https://forums.freertos.org/t/vsemaphorecreatebinary/3517/2 "2013-11-13T13:35:51Z")

</div>

**[richard\_damon](https://sourceforge.net/u/richard_damon/)** wrote on [Wednesday, November 13, 2013](https://sourceforge.net/p/freertos/discussion/382005/thread/9baa531d/#178c):

Not seeing the SemaphoreGive code, the only problems that I can see is you might be running out of heap.

What exactly do you comment out to make one side to work.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [November 13, 2013, 1:56pm UTC](https://forums.freertos.org/t/vsemaphorecreatebinary/3517/3 "2013-11-13T13:56:40Z")

</div>

**anonymous** wrote on [Wednesday, November 13, 2013](https://sourceforge.net/p/freertos/discussion/382005/thread/9baa531d/#e14f):

I have many tasks in my program all others are work fine. The problem is in these two tasks, i can use either one of this task SemaphoreGive() using some other tasks, i comment the create task  
////xTaskCreate( vReadTask, “Handler”, MEDIUM\_STACK\_SIZE, NULL, 11, &xReadHandler);

xTaskCreate(vCriticalTask,“Critical”, MEDIUM\_STACK\_SIZE,NULL,12,&xCriticalHandle);

this time CriticalTask will work

xTaskCreate( vReadTask, “Handler”, MEDIUM\_STACK\_SIZE, NULL, 11, &xReadHandler);

///xTaskCreate(vCriticalTask,“Critical”,MEDIUM\_STACK\_SIZE,NULL,12,&xCriticalHandle;

this time ReadTask will work.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [November 13, 2013, 2:03pm UTC](https://forums.freertos.org/t/vsemaphorecreatebinary/3517/4 "2013-11-13T14:03:19Z")

</div>

**anonymous** wrote on [Wednesday, November 13, 2013](https://sourceforge.net/p/freertos/discussion/382005/thread/9baa531d/#301d):

Yes, the error was in my configTOTAL\_HEAP\_SIZE i increased heap size problem solved. Thank you so much 🙂

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [November 13, 2013, 2:41pm UTC](https://forums.freertos.org/t/vsemaphorecreatebinary/3517/5 "2013-11-13T14:41:44Z")

</div>

**anonymous** wrote on [Wednesday, November 13, 2013](https://sourceforge.net/p/freertos/discussion/382005/thread/9baa531d/#6e1d):

void vReadTask( void \*pvParameters )  
{  
for( ;; )  
{  
xSemaphoreTake( xReadSemaphore, portMAX\_DELAY );  
//read code  
}  
}

Here the read code will execute first time (program starting) before giving a semaphore. why this happening? how to avoid this?

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [November 13, 2013, 2:59pm UTC](https://forums.freertos.org/t/vsemaphorecreatebinary/3517/6 "2013-11-13T14:59:39Z")

</div>

**[davedoors](https://sourceforge.net/u/davedoors/)** wrote on [Wednesday, November 13, 2013](https://sourceforge.net/p/freertos/discussion/382005/thread/9baa531d/#4076):

Look at the vSemaphoreCreateBinary() definition in semphr.h. You will see it is given after it is created so the first take will pass. If you don’t want that then take the semaphore straight after you have created it, before you use it in your task.

---

<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 13, 2013, 3:16pm UTC](https://forums.freertos.org/t/vsemaphorecreatebinary/3517/7 "2013-11-13T15:16:56Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Wednesday, November 13, 2013](https://sourceforge.net/p/freertos/discussion/382005/thread/9baa531d/#61bd):

The head revision in SVN currently has a new xSemaphoreCreateBinary() function that works like all the other semaphore create function (a function that returns the semaphore) so the vSemaphoreCreateBinary() macro (note with a ‘v’ prefix) can be retired - although it will remain in the source code for backward compatibility. The ‘x’ version creates an empty semaphore, so a ‘give’ must be performed before a ‘take’ will pass.

Regards.
