1
void forward(void *pvparam)
{
    while(1)
    {
        if(xSemaphoreTake(xSemaphore,1000)==pdTRUE)
        {
            UART0_SendStr("Frwd took it\n");
        }
        else
        { 
            UART0_SendStr("Frwd couldn't take it\n");
        }
        vTaskDelay(1000);
    }
}

void back(void *pvparam)
{
    vTaskDelay(100);
    while(1)
    {
        if(xSemaphoreGive(xSemaphore)==pdTRUE)
        {
            UART0_SendStr("Back Gave it:MF\n");
        }
        else
        { 
            UART0_SendStr("Back couldn't give it:MS\n");
        }
        vTaskDelay(1000);
    }
}

Above code is the one which i am using for both Binary semaphore and Mutex. The only difference is for binary semaphore i am writing "xSemaphoreCreateBinary(xsemaphore);" in main and for mutex xSemaphoreCreateMutex(xsemaphore) in main.

According to the definetion

"A semaphore(Mutex) occupied by the task can only be given by that Task and the Semaphore(Binary) created by a Task can be given by any of the Tasks"

But both the codes(i.e for binary semaphore and mutex) are giving same output.

1 Answer 1

2

Mutexes are used for controlling exclusive access to resources / data. If you take a mutex protecting the resource / data, you must give it back when you have finished, otherwise you will permanently block the resource. Mutexes also allow for priority inheritance.

Binary Semaphores on the other hand are used for task synchronization purposes. You do not give a give the semaphore back once you have taken it.

Looking at your code above I think that the Binary Semaphore is the way to go.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you,The problem was when i used the give function with a Mutex from another task the mutex was being released. I later realised the implementation level difference btw mutex and binary semaphore.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.