Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem TCP & FAT




Loading

Can xSemaphoreCreateMutex and xSemaphoreTake be in the same task

Posted by tof910 on February 4, 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


Can xSemaphoreCreateMutex and xSemaphoreTake be in the same task

Posted by rtel on February 4, 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.


Can xSemaphoreCreateMutex and xSemaphoreTake be in the same task

Posted by tof910 on February 4, 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.


[ Back to the top ]    [ About FreeRTOS ]    [ Sitemap ]    [ ]




Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.

Latest News

FreeRTOS kernel V10 is available for immediate download. Now MIT licensed.


FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

IAR Partner

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers

STMicro RTOS partner supporting ARM7, ARM Cortex-M3, ARM Cortex-M4 and ARM Cortex-M0

Texas Instruments MCU Developer Network RTOS partner for ARM and MSP430 microcontrollers

OpenRTOS and SafeRTOS