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


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 ]    [ 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