Quote Originally Posted by bithub View Post
Let's say 1 thread can call save_value() at the same time another thread can call print_value(). If you ask people to modify the code to make it thread safe, and yet still run as fast as possible, most people (including experts) will get it wrong.
This is a well known "race case". I would guess most programmers who have had some basic training in thread programming will do this correctly. If you use a mutex of some sort it will work. However if you don't, the fail in this case probably isn't that bad.

Even with single statements that read and write shared data you need to do something to make things work 100% correctly because you don't know what low level instructions the compiler is generating. Your issue seems to be "still run as fast as possible". If you if you want to take risks and sort cuts you may run in to problems and your program becomes machine or complier dependant.

However to get it to actually work reliably is easy if you take the accepted strategy.