considerations:
1) compiler optimization - do dates exist even as you planned? If this is not “externally observed” behavior, the abstract C / C ++ machine does not guarantee that the compiler will not optimize it. Your binary may not have a "dat", but instead you can write to a register, and threads will / may have different registers. Read the C / C ++ standard on an abstract machine or just google for “volatile” and explore from there. The C / C ++ standard takes care of a single thread, many threads can easily stumble over such optimization.
2) nuclear storage. Anything that has a chance to cross word boundaries will not be atomic. Int-s usually, if you do not pack them in a structure that has, for example, characters, and uses directives to remove the add-on. But you need to analyze this aspect every time. Explore your google platform to “populate”. Keep in mind that different CPUs have different rules.
3) problems with multiple CPUs. You wrote "dat" on CPU0. Will the change be visible even on CPU1? Or do you just write a local registry? Cache? Are the caches that connect you to your platform? Is access to a specific access guaranteed? Read on the "weak memory model." Gogle for "memory_barriers.txt Linux" is a good start.
4) use case. Do you intend to use "dat" after assignment - synchronized? But this, I think, is obvious.
Usually, "thread safety" does not go beyond guaranteeing that the function will work if it is called from different threads at the same time, but these calls should not be interdependent, that is, they do not exchange data regarding this call. For example, you call malloc () from thread1 and thread2, and both of them get memory, but they do not have access to other memory.
A counter example would be strtok (), which is not thread safe and will be interrupted even when calls are unconnected.
Once your threads start talking to each other over data, normal thread safety does not guarantee much.
n-alexander
source share