Quality RTOS & Embedded Software

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




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) 2004-2010 Richard Barry. Copyright (C) 2010-2016 Real Time Engineers Ltd.
Any and all data, files, source code, html content and documentation included in the FreeRTOSTM distribution or available on this site are the exclusive property of Real Time Engineers Ltd.. See the files license.txt (included in the distribution) and this copyright notice for more information. FreeRTOSTM and FreeRTOS.orgTM are trade marks of Real Time Engineers Ltd.

Latest News:

FreeRTOS V9.0.0 is now available for download.


Free TCP/IP and file system demos for the RTOS


Sponsored Links

⇓ Now With No Code Size Limit! ⇓
⇑ Free Download Without Registering ⇑


FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Renesas Electronics Gold Alliance RTOS Partner.jpg

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers

Atmel RTOS partner supporting ARM Cortex-M3 and AVR32 microcontrollers

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

Xilinx Microblaze and Zynq partner

Silicon Labs low power RTOS partner

Altera RTOS partner for Nios II and Cortex-A9 SoC

Freescale Alliance RTOS Member supporting ARM and ColdFire microcontrollers

Infineon ARM Cortex-M microcontrollers

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

Cypress RTOS partner supporting ARM Cortex-M3

Fujitsu RTOS partner supporting ARM Cortex-M3 and FM3

Microsemi (previously Actel) RTOS partner supporting ARM Cortex-M3

Atollic Partner

IAR Partner

Keil ARM Partner

Embedded Artists