Reputation: 1
I am setting up two threads for my application, and I am declaring a variable called x, which will be getting input from one thread and used in another thread for carrying out a function, as it is susceptible to change any time, I believe it has to be volatile and it needs to be global too. in this case can I declare a variable as static volatile x?
If yes, can someone shed some light on this ?
Upvotes: 0
Views: 174
Reputation: 25286
They are different concepts:
static
provides linkage information. It makes that the variable or function will only be known to the current compilation unit (source file). The name will not be in the object file.
volatile
tells the compiler the value of the variable may change from an outside souce or event. For example a flag that is set by an interrupt service routine when an interrupt has occurred. As a result, some compiler optimizations that assume the value of the variable does not change will be disabled.
So yes, a variable can be both static and volatile in a multi threaded environment.
Upvotes: 2