Can xSemaphoreCreateMutex and xSemaphoreTake be in the same task

tof910 wrote on Thursday, February 04, 2016:

Hello everyone,
My question is about the semaphore example here: http://www.freertos.org/a00122.html
Can the CreateMutex and xSemaphoreTake be in the same task.
I have have two task which potentially access to the same structure at the same time. Do I need to create a third task for the create mutex, or can the creation be in the task which handle the object, or in the main ?
Thank for your responses,
BR,
Flo

rtel wrote on Thursday, February 04, 2016:

I’m not fully understanding your explanation of why you think it might not be ok - but a single task can create and use multiple semaphores and mutexes, and the semaphores and mutexes can be used by any number of other tasks. Mutual exclusion is taken care of inside the API functions - so there is no need to take care of that in your application code.

tof910 wrote on Thursday, February 04, 2016:

Thanks for your reply. You almost answer to my question.

a single task can create and use multiple semaphores and mutexes.

Can the same mutex be create and then used in the same task ?
To illustrate my point, I modified the example code from the previous link into what i’d like to do:

SemaphoreHandle_t xSemaphore = NULL;

/* A task that creates a semaphore. */
void vATask( void * pvParameters )
{
    /* Create the semaphore to guard a shared resource.  As we are using
    the semaphore for mutual exclusion we create a mutex semaphore
    rather than a binary semaphore. */
    xSemaphore = xSemaphoreCreateMutex();
	
	/* .... */
	
	if( xSemaphore != NULL )
    {
        /* See if we can obtain the semaphore.  If the semaphore is not
        available wait 10 ticks to see if it becomes free. */
        if( xSemaphoreTake( xSemaphore, ( TickType_t ) 10 ) == pdTRUE )
        {
            /* We were able to obtain the semaphore and can now access the
            shared resource. */

            /* ... */

            /* We have finished accessing the shared resource.  Release the
            semaphore. */
            xSemaphoreGive( xSemaphore );
        }
        else
        {
            /* We could not obtain the semaphore and can therefore not access
            the shared resource safely. */
        }
    }

	
	
}

/* A task that uses the semaphore. */
void vAnotherTask( void * pvParameters )
{
    /* ... Do other things. */

    if( xSemaphore != NULL )
    {
        /* See if we can obtain the semaphore.  If the semaphore is not
        available wait 10 ticks to see if it becomes free. */
        if( xSemaphoreTake( xSemaphore, ( TickType_t ) 10 ) == pdTRUE )
        {
            /* We were able to obtain the semaphore and can now access the
            shared resource. */

            /* ... */

            /* We have finished accessing the shared resource.  Release the
            semaphore. */
            xSemaphoreGive( xSemaphore );
        }
        else
        {
            /* We could not obtain the semaphore and can therefore not access
            the shared resource safely. */
        }
    }
}

Thanks.