Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8631227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:11:25+00:00 2026-06-12T09:11:25+00:00

I read about usage of C volatile keyword in memory-mapped hardware register, ISR, and

  • 0

I read about usage of C volatile keyword in memory-mapped hardware register, ISR, and multithreaded program.

1) register

uint8_t volatile * pReg;
while (*pReg == 0) { // do sth } // pReg point to status register 

2) ISR

int volatile flag = 0;
int main() 
{
    while(!flag) { // do sth }
}
interrupt void rx_isr(void)
{
    //change flag
}

3) multithread

int volatile var = 0;
int task1()
{
    while (var == 0) { // do sth }
}
int task2()
{
    var++;
}

I can see why compiler can mistakenly optimize the while in case 1) if volatile is not there, ’cause variable change is made from hardware, compiler may not see any change of the variable made from code.

But for case 2) and 3), why is volatile ever needed? In both cases variable is declared global, and compiler can see it’s used in more than one place. So why would compiler optimize the while loop if the variable is not volatile?

Is it because a compiler by-design has no idea of “asynchronous call” (in case of ISR), or multithreading? But this can’t be, right?

In addition, case 3) looks like a common program in multithreading without the volatile keyword. Let’s say I add some locking to the global variable (no volatile keyword):

int var = 0;
int task1()
{
    lock();   // some mutex
    while (var == 0) { do sth }
    release()
}
int task2()
{
    lock();
    var++;
    release();
}

It looks normal enough to me. So do I really need volatile in multithreading? How come I’ve never seen volatile qualifier added to variable to avoid optimization in multithread program before?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T09:11:26+00:00Added an answer on June 12, 2026 at 9:11 am

    The main point of using volatile keyword is to prevent compiler from generating a code that uses CPU registers as faster ways to represent variables. This forces compiled code to access the exact memory location in RAM on every access to the variable to get the latest value of it which may have been changed by another entity. By adding volatile we make sure that our code is aware of any change made to a variable by anyone else like hardware or ISR and no coherency issue happens.

    In absence of volatile keyword, compiler tries to generate faster code by reading the content of variable from RAM into a CPU register once and use that cached value in a loop or function. Accessing RAM could be tens of times slower than accessing the CPU register.

    I’ve had the experience on item 1 and 2 but I don’t think you need to define a variable as volatile in a multi threded environment. Adding the lock/unlock mechanism is necessary to solve synchronization problem and is not related the what volatile is about.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I read some articles about the volatile keyword but I could not figure out
I have read about the memory usage of a wcf client and what i
On this post , I read about the usage of XMPP. Is this sort
Quite recently I read about JavaScript call usage in MDC https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/call one linke of
I had read article about how stackoverflow reduced its CPU usage by using lucene.
I have read many tutorials on the internet about the usage of the 'tr'
I've a question about KConfig usage. I'm able to write and read settings in
Read about Server push here . I want to push data to client from
Read about the issue in this stackoverflow question . Still have the same issue
Ive read about it and to be honest it all seems like a bunch

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.