Why we do not use barriers in User space

Viewed 1495

I am reading about memory barriers and what I can summarize is that they prevent instruction re-ordering done by compilers.

So in User space memory lets say I have

b = 0;
main(){

a = 10;
b = 20;
c = add(a,b);

}

Can the compiler reorder this code so that b = 20 assignment happens after c = add() is called.

Why we do not use barriers in this case ? Am I missing some fundamental here.

Does Virtual memory is exempted from any re ordering ?

Extending the Question further:

In Network driver:

1742         /*
1743          * Writing to TxStatus triggers a DMA transfer of the data
1744          * copied to tp->tx_buf[entry] above. Use a memory barrier
1745          * to make sure that the device sees the updated data.
1746          */
1747         wmb();
1748         RTL_W32_F (TxStatus0 + (entry * sizeof (u32)),
1749                    tp->tx_flag | max(len, (unsigned int)ETH_ZLEN));
1750 

When he says devices see the updated data... How to relate this with the multi threaded theory for usage of barriers.

3 Answers
Related