search

Home  >  Q&A  >  body text

c++ - 关于_int64的一点疑问

在百度百科上关于_int64这种数据类型有这么一句话:

警告 在 32 位 Intel 计算机上分配 64 位值不是原子操作;即该操作不是线程安全的。这意味着,如果两个人同时将一个值分配给一个静态
Int64 字段,则该字段的最终值是无法预测的。

不是很理解这段话的意思。。。有什么通俗简单的解释么?

天蓬老师天蓬老师2774 days ago395

reply all(3)I'll reply

  • PHP中文网

    PHP中文网2017-04-17 13:52:07

    On a 32-bit computer, assigning or getting a value to a variable of type _int64 is a two-step operation at the CPU instruction level, writing or reading the first 32 bits and the last 32 bits of the memory respectively. If other threads read and write this variable between these two CPU instructions, concurrency problems will occur.
    For example:
    Thread 1 first writes the first 32 bits of the variable,
    then thread 2 writes the first 32 bits and last 32 bits of the variable,
    then thread 1 writes the last 32 bits of the variable 32 bits.
    The final result is that the first 32 bits of the memory are written by thread 2, and the last 32 bits are written by thread 1. The data in memory ends up being a completely wrong data.

    reply
    0
  • PHPz

    PHPz2017-04-17 13:52:07

    This is multi-threaded, which means that Int64 is not an atomic operation. Common concurrency problems will occur during multi-threaded operations

    reply
    0
  • ringa_lee

    ringa_lee2017-04-17 13:52:07

    Intel’s 32-bit CPU ensures that no matter how many threads are accessing the memory, only one thread is accessing the same byte, double byte, or quad byte at the same time.
    Use a 32-bit CPU to read and write eight-byte data. When most compilers implement this function, they read and write four-byte data twice from a programming perspective. So - you can't guarantee that when you read and write the first four-byte data, other threads will be writing and reading the second four-byte data.

    reply
    0
  • Cancelreply