0

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 ?

curiousguy
  • 8,038
  • 2
  • 40
  • 58
john
  • 1
  • 3
  • Have you tried? – John Dvorak Nov 05 '19 at 14:12
  • it works fine without volatile, and with volatile, but I am just wondering is there any difference – john Nov 05 '19 at 14:13
  • 2
    ["`volatile` is (nearly) useless for platform-agnostic, multithreaded application programming. It does not provide any synchronization, it does not create memory fences, nor does it ensure the order of execution of operations. ...](https://stackoverflow.com/questions/4557979/when-to-use-volatile-with-multi-threading) – Andrew Henle Nov 05 '19 at 14:18
  • "volatile" only prevents some compiler optimizations. It's unlikely it will have an effect without optimizations on. – John Dvorak Nov 05 '19 at 14:18
  • 2
    @JohnDvorak: Experimentation is not a valid method of determining whether something works in a multithreaded environment, as thread timing and behavior may vary, so something that works in repeated experiments may fail in deployment. – Eric Postpischil Nov 05 '19 at 14:18
  • Both might not work, you need proper synchronisation mechanisms. Reads this: https://stackoverflow.com/questions/2484980/why-is-volatile-not-considered-useful-in-multithreaded-c-or-c-programming – Jabberwocky Nov 05 '19 at 14:18
  • 2
    *it works fine without volatile, and with volatile* No, you just haven't **observed** it failing. Yet. – Andrew Henle Nov 05 '19 at 14:20
  • @EricPostpischil Experimentation is a valid method of determining whether something compiles, which is what the asker seemed to be asking about – John Dvorak Nov 05 '19 at 14:46
  • Related: https://stackoverflow.com/questions/24954526/how-can-an-auto-variable-be-volatile – harper Nov 05 '19 at 15:18

1 Answers1

2

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.

Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
  • 1
    You should also tell is that's it's not OK if 2 threads access a common variable without proper synchronisation (see comments) – Jabberwocky Nov 05 '19 at 14:35
  • yes but in this case one thread will use the global variable to get the input, where as the other thread will use input to do the required function in other thread, So I don't think I need any mutex or semaphore to protect the shared varaible @Jabberwocky – john Nov 05 '19 at 21:10
  • @john it's not about about protection, mutexes or semaphore, but it's about memory barriers, have a look at the links in the comments of your question. – Jabberwocky Nov 06 '19 at 06:48