Quality RTOS & Embedded Software

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


Global Vairable handing in FreeRTOS

Posted by gauravpatni on July 21, 2017

Dear All,

We have just started development based on Free RTOS .But we are unable to find 'recommended' way of handling global variable . Please refer following case :

External int is use for counting digital sensor values .

multiple task need to get this digital sensor count for further processing ( read only ) .

As may times i read that use of global vairable is crude method in multitasking system . So request you to please help me by suggesting suitable / recommended method .

Regards, Gaurav

Global Vairable handing in FreeRTOS

Posted by rtel on July 21, 2017

If you have one writer and multiple readers, and the update to the variable is atomic (i.e. the variable is updated in one write, like a 32-bit value being updated in a single 32-bit write as opposed to two 16-bit writes) then an external variable is a very efficient way of doing what you want. You could get issues with multiple writer scenarios though.

Global Vairable handing in FreeRTOS

Posted by gauravpatni on July 21, 2017

Thanks for quick reply .

"external variable" means Global variable ?

also please let me know the recommended way for implementing "multiple writer " & " multiple reader " . This will be helpful for future application development .

Regards, Gaurav

Global Vairable handing in FreeRTOS

Posted by richard_damon on July 21, 2017

Gaurav, When you get into Multiple Writers, Multiple Readers, the big question comes to what do you mean by this and what are behavior are you expecting. At the simple level, it still will just work with a simple atomic int. The variable will have the value last written when every reader reads it. That means if one task writes a value of 5, then another task writes the value 10, after that point all readers will see the 10, and a reader which reads between the two writes will see a 5. If there is no interactions between the writers to synchronize the writes, then it is possible that under other conditions, the 10 might have occurred first, and then the final value would be the 5, so you have some uncertainty due to the 'race' condition. Similarly for the readers, if there is nothing synchronizing them to the writers, eventually they will see the later value, but if they happen to read before the last write, they will see the earlier write. Another 'race' condition.

Normally, this sort of variability isn't desired, but there are conditions where it might be acceptable. Thus definition is needed.

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

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

Latest News

NXP tweet showing LPC5500 (ARMv8-M Cortex-M33) running FreeRTOS.

Meet Richard Barry and learn about running FreeRTOS on RISC-V at FOSDEM 2019

Version 10.1.1 of the FreeRTOS kernel is available for immediate download. MIT licensed.

View a recording of the "OTA Update Security and Reliability" webinar, presented by TI and AWS.


FreeRTOS and other embedded software careers at AWS.

FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Espressif ESP32

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

Xilinx Microblaze and Zynq partner