标签:
The simple thread example we showed above was useful in showing how threads are
created and how they can run in different orders depending on how the scheduler decides
to run them. What it doesn‘t show you, though, is how threads interact when they access
shared data.
The Heart Of The Problem: Uncontrolled Scheduling
To understand why this happens, we must understand the code sequence that the compiler
generates for the update to counter. In this case, we wish to simple add a number 1 to counter.
Thus, the code sequence for doing so might look something like this (in X86);
mov 0x8049a1c, %eax
add $0x01, %eax
mov %eax, 0x8049a1c
This example assumes that the variable counter is located at address 0x8049a1c. In this
three-instruction sequence, the x86 mov instruction is used first to get the memory value at
the address and put it into register eax. Then, the add is performed, adding 1 to the contents
of the eax register, and finally, the contents of eax are stored back into memory at the same
address.
Operating System: Three Easy Pieces --- Why It Gets Worse: Shared Data (Note)
标签:
原文地址:http://www.cnblogs.com/miaoyong/p/4942370.html