Out-of-Order Execution
Each thread has its own Instruction Pointer.
Consider for a moment only a single thread.
The instructions are read in sequentially,
but are not necessarily executed in that order.
Processors use a technique called
pipelining
in which an instruction starts executing before
a previous instruction finishes execution.
The previous instruction may take a long time
and the next instruction could actually finish first.
That is just a simple explanation. There are actually
variations of this. For example,
to produce more efficient machine code
the compiler could
change the order of execution of your source code
(static optimization) without disrupting dependencies
between your variables: quicker instructions
could be batched together from nearby code if
other variables are not affected.
Or in dynamic scheduling, hardware instruction buffers are used,
where instructions wait for their source operands to
become available.
|
An instruction may issue for execution as soon as
its operands become available. Thus prior stalled instructions
do not stall later independent instructions. [ 1 ]
|
|
After execution, the instructions are
commimitted (reordered to original order)
in case an interrupt has to be processed. [ 2 ]
And so the actual order of execution is
irrelevant, because dependencies were
tracked while the instructions were out-of-order.
But this is only true within a thread.
Dependencies between threads are not tracked.
It is up to you, the application programmer,
to track dependencies between threads.
|
 |
|
1.
|
Eric Rotenberg and Aravindh Anantaraman,
Architecture of Embedded Microprocessors,
in Jerraya & Wolf (Eds.),
Multiprocessor Systems-on-Chips
(Morgan Kaufmann / Elsevier, 2005),
p. 91
|
|
|
|
2.
|
Silc, Robic and Ungerer,
Processor Architecture
(Springer, 1999),
p. 127.
|
|