Under C# is Int64 use on a 32 bit processor dangerous

前端 未结 6 1242
失恋的感觉
失恋的感觉 2020-12-31 03:41

I read in the MS documentation that assigning a 64-bit value on a 32-bit Intel computer is not an atomic operation; that is, the operation is not thread safe. This means tha

6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-31 04:27

    On a 32-bit x86 platform the largest atomic sized piece of memory is 32-bits.

    This means that if something writes to or reads from a 64-bit sized variable it's possible for that read/write to get pre-empted during execution.

    • For example, you start to assign a value to a 64 bit variable.
    • After the first 32 bits are written the OS decides that another process is going to get CPU time.
    • The next process attempts to read the variable you were in the middle of assigning to.

    That's just one possible race condition with 64-bit assignment on a 32 bit platform.

    However, even with 32 bit variable there can be race conditions with reading and writing therefor any shared variable should be synchronized in some way to solve these race conditions.

提交回复
热议问题