c - Should I mutex lock a single variable? -


if single 32-bit variable shared between multiple threads, should put mutex lock around variable? example, suppose 1 thread writes 32-bit counter , 2nd thread reads it. there chance 2nd thread read corrupted value?

i'm working on 32-bit arm embedded system. compiler seems align 32-bit variables can read or written single instruction. if 32-bit variable not aligned, read or write broken down multiple instructions , 2nd thread read corrupted value.

does answer question change if move multiple-core system in future , variable shared between cores? (assuming shared cache between cores)

thanks!

a mutex protects more tearing - example arm implementations use out-of-order execution, , mutex include memory (and compiler) barriers may necessary algorithm's correctness.

it safer include mutex, figure out way optimise later if shows performance problem.

note if compiler gcc-based, may have access gcc atomic builtins.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -