views:

255

answers:

4

I am looking for things like reordering of code that could even break the code in the case of a multiple processor.

A: 

Yes, but what exactly is your question?

However, since this is an interesting topic: tricks that compilers and processors use to optimize code should not break code, even with multiple processors, in the absence of race conditions in that code. This is called the guarantee of sequential consistency: if your program does not have any race conditions, and all data is correctly locked before accessing, the code will behave as if it were executed sequentially.

There is a really good video of Herb Sutter talking about this here:

http://video.google.com/videoplay?docid=-4714369049736584770

Everyone should watch this :)

DavidK
+1  A: 

Wikipedia has a fairly comprehensive list of optimization techniques here.

Robert Gamble
+2  A: 

The most important one would be memory access reordering.

Absent memory fences or serializing instructions, the processor is free to reorder memory accesses. Some processor architectures have restrictions on how much they can reorder; Alpha is known for being the weakest (i.e., the one which can reorder the most).

A very good treatment of the subject can be found in the Linux kernel source documentation, at Documentation/memory-barriers.txt.

Most of the time, it's best to use locking primitives from your compiler or standard library; these are well tested, should have all the necessary memory barriers in place, and are probably quite optimized (optimizing locking primitives is tricky; even the experts can get them wrong sometimes).

CesarB
Related question: http://stackoverflow.com/questions/286629/what-is-mean-by-memory-fences
CesarB
A: 

DavidK's answer is correct, however it is also very important to be aware of the memory model for your language/runtime. Even without race conditions and with sequential consistency and mutex usage your code can still break when data is being cached by different threads running in the different cores of the cpu. Some languages, Java is one example, ensure the state of data between threads when a mutex lock is used, but it is rarely enough to simply ensure that no two threads can access the data at the same time. You need to use the mutex in a correct way to ensure that the language runtime synchronizes the data state between the two threads. In java this is done by having the two threads synchronize on the same object.

Here is a good page explaining the problem and how it's dealt with in javas memory model.

http://gee.cs.oswego.edu/dl/cpj/jmm.html

kasperjj