Quality RTOS & Embedded Software

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




Loading

Protecting variable types using mutexes question

Posted by genarisimo23 on February 18, 2015

Hi all, It is neccesary to use mutexes or another method to protect this kind of variables?

volatile bool bflag = FALSE;

Task1: ... bflag = TRUE; ...

Task2: ... if (bflag == TRUE) {
bFlag = FALSE; } ...

It is this the following solution OK? Task1: bool localflag; TakeMutex(); localflag = bFlag; GiveMutex(); if (localflag == TRUE) { localflag = FALSE; }

Task2:

TakeMutex(); bFlag = TRUE; GiveMutex();

Could be any difference between using boolean type var and any other type?

Thanks in advance


Protecting variable types using mutexes question

Posted by rtel on February 18, 2015

If one task was only reading, and one task was only writing, then you would not need any mutual exclusion. As it is both tasks are writing to the variable, so you might need mutual exclusion.

If both tasks could write to the variable at the same time then you would need some form of mutual exclusion.

If only one task could write to the variable at a time then you would not need mutual exclusion. For example if Task 1 only wrote when the variable was FALSE, and task 2 only wrote when the variable is TRUE.

In any case a mutex is way too heavy for just setting a variable. Writing to a variable is very quick, so it is probably more appropriate to use a simple critical section.

taskENTERCRITICAL(); bFlag = TRUE; taskEXITCRITICAL();

Regards.


Protecting variable types using mutexes question

Posted by genarisimo23 on February 18, 2015

Thanks for your quick response!

Regards


[ 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