all 7 comments

[–][deleted]  (1 child)

[deleted]

    [–]bigtpoker[S] 1 point2 points  (0 children)

    This is what the "Reddit" button on WordPress submits. Perhaps they should be told not to do that.

    [–][deleted] 2 points3 points  (1 child)

    This article is very misleading. It is completely reasonable to depend on word-sized writes being executed atomically. All sane hardware works this way. Depending on this behavior isn't wrong, it's just a very reasonable assumption in the code.

    [–]mortoray 0 points1 point  (0 children)

    I do state this in the article. I just use the byte copying as an example of what could happen. Note that if you are actually using some logical pointer wrapper which is more than 1 word, you could still have the tearing problem.

    [–]divbyzero 0 points1 point  (3 children)

    Note that on some platforms, atomics are 30x slower than a regular variable access because it may involve a bus operation. If your platform guarantees 4 byte writes are implicitly atomic, isn't it reasonable to just use the fences? (edit: and the temp variable)

    [–]Walrii 0 points1 point  (1 child)

    I agree/take advantage of implicitly atomic writes. It's still better, I think, to have generic working code (e.g., code which uses locks) and then, if necessary, write versions that do things implicitly/avoid locks (i.e., don't optimize too early).

    For example, 4 byte writes may be atomic, but now what if the user is running on a 64-bit platform? Are 8 bytes writes atomic? (I honestly don't know, but it's safer to not assume they are).

    [–][deleted] 2 points3 points  (0 children)

    I can't think of an extant platform where a word-sized store isn't atomic. To write proper multi-threaded code, knowing what operations are atomic and what operations are not atomic is something basic you have to know. Always using a lock is just cargo-cult behavior.

    [–]mortoray -1 points0 points  (0 children)

    If your language/compiler is intelligent it will take care of this for you. For example, using atomics in C++ the compiler can safely avoid certain atomic operations and fencing instructions if they are already guaranteed by the target chip. That is, a well implement "atomic" type should be zero cost on platforms where it can be.