Should I use "Volatile" in the following case?

Please note that my application has several .c source files.

Module1.c

static char status_variable = 0;

Modify1() 
{

   status_variable = 'a';

}

Modify2() 
{

   status_variable = 'x';

}

char GetStatus()
{
   return status_variable;
}

      

Function Modify

can be called from other .c files based on events (not via ISR)

Module2.C

TakeAction()
 {
    if(GetStatus() == 'a')
    {
        //do something
    }
    else 
    {
        //do something
    }

}

      

Now my question is, do I need to declare status_variable

like volatile

in this case?

EDIT1:

My application is a 16-bit microcontroller (RL78), I am not using any operating system.

EDIT2:

As some are commenting, multithreading was not in my mind. I have written down a simple uncaught circular motion scheduler for my application. I have no idea about multi-threaded environment and what makes the variable volatile in this case. How is it different from my environment? If someone can develop it, it will be of great help.

+3


source to share


2 answers


Since it is status_variable

not attached to any:

  • Listed peripheral registers with memory
  • Global variables modified by the interrupt service
  • Global variables in a multithreaded application


You can omit a keyword volatile

in your ad status_variable

.

+4


source


You should definitely use volatile if modules can run concurrently as @ BlueMoon93 said (unless there is another locking mechanism for threads). But if these modules need to run one after the other, you can safely skip using mutable.



+1


source







All Articles