Quality RTOS & Embedded Software

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


Volatile variables

Posted by miikalo on August 27, 2009

Is it necessary or encouraged to use volatile variables when a global variable is shared by a lower priority and a higher priority task? Thanks for your replies.

RE: Volatile variables

Posted by dave m on August 27, 2009
"Sometimes," or "it depends." It's a compiler question, not a FreeRTOS issue. If a task that uses the variable does not call any subroutines (or if the compiler can prove that the subroutines it does call never alter the variable), then the optimizer may put the variable in a register temporary, or move the test outside a loop, or something like that. The "volatile" keyword tells the compiler that it must read (and write) the variable every time the program says to do so. (The precise requirements that the compiler must meet are quite a bit more complex -- I think it may also have to ensure that volatile reads and writes happen in the same order as the program, and possibly that other memory accesses, even non-volatile ones, have completed before [or begin after] the volatile access.)

Task priority does not come into it at all. Compilers usually don't do anything different for threaded/tasked programs, and the RTOS ensures that each task can't detect context switches (registers & stack never change in ways that the compiler doesn't expect).

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

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

Latest News

FreeRTOS v10.2.1 is available for immediate download. MIT licensed, includes 64-bit RISC-V, NXP Cortex-M33 demo, Nuvoton Cortex-M23 demo & STM32H745 dual core (AMP) demo.

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

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

Cadence Tensilica Cortes

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