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

后端 未结 16 1716
野性不改
野性不改 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:17

    They are both thread-unsafe.

    A CPU cannot do math directly with memory. It does that indirectly by loading the value from memory and doing the math with CPU registers.

    i++

    register int a1, a2;
    
    a1 = *(&i) ; // One cpu instruction: LOAD from memory location identified by i;
    a2 = a1;
    a1 += 1; 
    *(&i) = a1; 
    return a2; // 4 cpu instructions
    

    ++i

    register int a1;
    
    a1 = *(&i) ; 
    a1 += 1; 
    *(&i) = a1; 
    return a1; // 3 cpu instructions
    

    For both cases, there is a race condition that results in the unpredictable i value.

    For example, let's assume there are two concurrent ++i threads with each using register a1, b1 respectively. And, with context switching executed like the following:

    register int a1, b1;
    
    a1 = *(&i);
    a1 += 1;
    b1 = *(&i);
    b1 += 1;
    *(&i) = a1;
    *(&i) = b1;
    

    In result, i doesn't become i+2, it becomes i+1, which is incorrect.

    To remedy this, moden CPUs provide some kind of LOCK, UNLOCK cpu instructions during the interval a context switching is disabled.

    On Win32, use InterlockedIncrement() to do i++ for thread-safety. It's much faster than relying on mutex.

    0 讨论(0)
  • 2020-11-27 11:17

    If you are sharing even an int across threads in a multi-core environment, you need proper memory barriers in place. This can mean using interlocked instructions (see InterlockedIncrement in win32 for example), or using a language (or compiler) that makes certain thread-safe guarantees. With CPU level instruction-reordering and caches and other issues, unless you have those guarantees, don't assume anything shared across threads is safe.

    Edit: One thing you can assume with most architectures is that if you are dealing with properly aligned single words, you won't end up with a single word containing a combination of two values that were mashed together. If two writes happen over top of each other, one will win, and the other will be discarded. If you are careful, you can take advantage of this, and see that either ++i or i++ are thread-safe in the single writer/multiple reader situation.

    0 讨论(0)
  • 2020-11-27 11:18

    Even if it is reduced to a single assembly instruction, incrementing the value directly in memory, it is still not thread safe.

    When incrementing a value in memory, the hardware does a "read-modify-write" operation: it reads the value from the memory, increments it, and writes it back to memory. The x86 hardware has no way of incrementing directly on the memory; the RAM (and the caches) is only able to read and store values, not modify them.

    Now suppose you have two separate cores, either on separate sockets or sharing a single socket (with or without a shared cache). The first processor reads the value, and before it can write back the updated value, the second processor reads it. After both processors write the value back, it will have been incremented only once, not twice.

    There is a way to avoid this problem; x86 processors (and most multi-core processors you will find) are able to detect this kind of conflict in hardware and sequence it, so that the whole read-modify-write sequence appears atomic. However, since this is very costly, it is only done when requested by the code, on x86 usually via the LOCK prefix. Other architectures can do this in other ways, with similar results; for instance, load-linked/store-conditional and atomic compare-and-swap (recent x86 processors also have this last one).

    Note that using volatile does not help here; it only tells the compiler that the variable might have be modified externally and reads to that variable must not be cached in a register or optimized out. It does not make the compiler use atomic primitives.

    The best way is to use atomic primitives (if your compiler or libraries have them), or do the increment directly in assembly (using the correct atomic instructions).

    0 讨论(0)
  • 2020-11-27 11:18

    I think that if the expression "i++" is the only in a statement, it's equivalent to "++i", the compiler is smart enough to not keep a temporal value, etc. So if you can use them interchangeably (otherwise you won't be asking which one to use), it doesn't matter whichever you use as they're almost the same (except for aesthetics).

    Anyway, even if the increment operator is atomic, that doesn't guarantee that the rest of the computation will be consistent if you don't use the correct locks.

    If you want to experiment by yourself, write a program where N threads increment concurrently a shared variable M times each... if the value is less than N*M, then some increment was overwritten. Try it with both preincrement and postincrement and tell us ;-)

    0 讨论(0)
提交回复
热议问题