Quantcast
Channel: Forum - Recent Threads
Viewing all articles
Browse latest Browse all 5781

Is a global variable thread safe ???

$
0
0

I know I am speaking heresy but in industrial controls we often would like to use global variables for process values (temperature, etc.) sensor status, etc.  Mutex usage complicates the code and does not help readability for the code.  Usually our global variables are written in one thread and read by many threads.

Here is why I think global variables can be thread safe:

  • I am only talking about this in the context of Synergy with 32 bit microcontrollers
  • Any global variable should only be written to directly i.e never read/modify/write (+=, -= etc)
  • No global variable should be more than 4 bytes
  • I have checked the IAR compiler and I think the GCC compiler is the same.  Unless you work really hard in options (packing) all variables are aligned on even 4 byte addresses
  • Reads and writes to variables on even boundries are one assembly language instruction.
  • If it is one assembly language instruction it would be impossible for an irq or thread preemption to corrupt the write or read
  • Therefore global variables should be threadsafe

Thoughts any one??? (other than complaints about my heretical thinking!!!)

Thanks,

Steve D


Viewing all articles
Browse latest Browse all 5781

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>