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

后端 未结 16 1728
野性不改
野性不改 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 10:54

    You say "it's only one instruction, it'd be uninterruptible by a context switch." - that's all well and good for a single CPU, but what about a dual core CPU? Then you can really have two threads accessing the same variable at the same time without any context switches.

    Without knowing the language, the answer is to test the heck out of it.

    0 讨论(0)
  • 2020-11-27 10:56

    You've heard wrong. It may well be that "i++" is thread-safe for a specific compiler and specific processor architecture but it's not mandated in the standards at all. In fact, since multi-threading isn't part of the ISO C or C++ standards (a), you can't consider anything to be thread-safe based on what you think it will compile down to.

    It's quite feasible that ++i could compile to an arbitrary sequence such as:

    load r0,[i]  ; load memory into reg 0
    incr r0      ; increment reg 0
    stor [i],r0  ; store reg 0 back to memory
    

    which would not be thread-safe on my (imaginary) CPU that has no memory-increment instructions. Or it may be smart and compile it into:

    lock         ; disable task switching (interrupts)
    load r0,[i]  ; load memory into reg 0
    incr r0      ; increment reg 0
    stor [i],r0  ; store reg 0 back to memory
    unlock       ; enable task switching (interrupts)
    

    where lock disables and unlock enables interrupts. But, even then, this may not be thread-safe in an architecture that has more than one of these CPUs sharing memory (the lock may only disable interrupts for one CPU).

    The language itself (or libraries for it, if it's not built into the language) will provide thread-safe constructs and you should use those rather than depend on your understanding (or possibly misunderstanding) of what machine code will be generated.

    Things like Java synchronized and pthread_mutex_lock() (available to C/C++ under some operating systems) are what you need to look into (a).


    (a) This question was asked before the C11 and C++11 standards were completed. Those iterations have now introduced threading support into the language specifications, including atomic data types (though they, and threads in general, are optional, at least in C).

    0 讨论(0)
  • 2020-11-27 10:59

    For a counter, I recommend a using the compare and swap idiom which is both non locking and thread-safe.

    Here it is in Java:

    public class IntCompareAndSwap {
        private int value = 0;
    
        public synchronized int get(){return value;}
    
        public synchronized int compareAndSwap(int p_expectedValue, int p_newValue){
            int oldValue = value;
    
            if (oldValue == p_expectedValue)
                value = p_newValue;
    
            return oldValue;
        }
    }
    
    public class IntCASCounter {
    
        public IntCASCounter(){
            m_value = new IntCompareAndSwap();
        }
    
        private IntCompareAndSwap m_value;
    
        public int getValue(){return m_value.get();}
    
        public void increment(){
            int temp;
            do {
                temp = m_value.get();
            } while (temp != m_value.compareAndSwap(temp, temp + 1));
    
        }
    
        public void decrement(){
            int temp;
            do {
                temp = m_value.get();
            } while (temp > 0 && temp != m_value.compareAndSwap(temp, temp - 1));
    
        }
    }
    
    0 讨论(0)
  • 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).

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

    Never assume that an increment will compile down to an atomic operation. Use InterlockedIncrement or whatever similar functions exist on your target platform.

    Edit: I just looked up this specific question and increment on X86 is atomic on single processor systems, but not on multiprocessor systems. Using the lock prefix can make it atomic, but it's much more portable just to use InterlockedIncrement.

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

    On x86/Windows in C/C++, you should not assume it is thread-safe. You should use InterlockedIncrement() and InterlockedDecrement() if you require atomic operations.

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