| ||||
| Comment on GotW #95 Solution: Thread Safety and Synchronization by Herb Sutter
@xiongzou: No, ints (and even chars) can get trashed in a race. Synchronization is not only about atomicity, it's also about ordering and disabling some optimizations. If you want a standalone mutable shared int that is safe to use concurrently from multiple threads, it must be at least an atomic or part of data protected by a mutex. Otherwise, in a data race even an int can get trashed, such as by an optimizer that thinks only its thread is using the int and performs normal legal single-threaded optimizations, such as injecting speculative writes which in a race could cause other threads to see “impossible” values. Read More »Comment on GotW #96: Oversharing by Michael Golub
1. a. Increased ratio between boilerplate code and actual logic makes code more complex. Comment on GotW #95 Solution: Thread Safety and Synchronization by xiongzou
I am a bit confused, in most modern CPUs, atomic operation is guranteed on a word size memory with read and write operations. int is a word size in most operating systems, which should be safe from race condition in your example, right? Not sure I mis-understand anything here? Thanks Read More »Comment on GotW #95 Solution: Thread Safety and Synchronization by Bartosz Milewski
@xiongzou: Atomic operations also guarantee ordering: all threads see changes to atomic variables in the same order (it’s called sequential consistency and is only guaranteed for strong atomics). A simple example of a race is when a thread modifies one shared variable “x” and then sets a shared flag “done.” You want other threads to see those writes in the same order, because if they see “done” before the modification to “x”, they will work with stale data. This is *not* guaranteed if the flag is not atomic. The compiler is free to reorder writes to different variables, and the processor is free to hold some writes in local buffers before committing them to global memory. So atomic variables are not only about atomicity but also about visibility. Read More » | ||||
| | ||||
| ||||
Monday, January 20, 2014
FeedaMail: Comments for Sutterâs Mill
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment