I've heard i++ isn't thread safe, is ++i thread-safe?

后端 未结 16 1868
野性不改
野性不改 2020-11-27 10:38

I\'ve heard that i++ isn\'t a thread-safe statement since in assembly it reduces down to storing the original value as a temp somewhere, incrementing it, and then replacing

16条回答
  •  囚心锁ツ
    2020-11-27 11:01

    If your programming language says nothing about threads, yet runs on a multithreaded platform, how can any language construct be thread-safe?

    As others pointed out: you need to protect any multithreaded access to variables by platform specific calls.

    There are libraries out there that abstract away the platform specificity, and the upcoming C++ standard has adapted it's memory model to cope with threads (and thus can guarantee thread-safety).

提交回复
热议问题