pthreads: If I increment a global from two different threads, can there be sync issues?

后端 未结 4 1323
再見小時候
再見小時候 2020-12-19 12:53

Suppose I have two threads A and B that are both incrementing a ~global~ variable \"count\". Each thread runs a for loop like this one:

for(int i=0; i<100         


        
4条回答
  •  清酒与你
    2020-12-19 13:54

    Count clearly needs to be protected with a mutex or other synchronization mechanism.

    At a fundamental level, the count++ statment breaks down to:

    load count into register
    increment register
    store count from register
    

    A context switch could occur before/after any of those steps, leading to situations like:

    Thread 1:  load count into register A (value = 0)
    Thread 2:  load count into register B (value = 0)
    Thread 1:  increment register A (value = 1)
    Thread 1:  store count from register A (value = 1)
    Thread 2:  increment register B (value = 1)
    Thread 2:  store count from register B (value = 1)
    

    As you can see, both threads completed one iteration of the loop, but the net result is that count was only incremented once.

    You probably would also want to make count volatile to force loads & stores to go to memory, since a good optimizer would likely keep count in a register unless otherwise told.

    Also, I would suggest that if this is all the work that's going to be done in your threads, performance will dramatically drop from all the mutex locking/unlocking required to keep it consistent. Threads should have much bigger work units to perform.

提交回复
热议问题