What are all the things one needs to be careful about when coding in a multicore environment?
For example, for a singleton class, it is better to create a global object and then return its reference than a static object.
i.e Rather than having
MyClass & GetInstance() { static Myclass singleMyclass; return singleMyclass; }
It is better to have
Myclass singleMyclass; MyClass & GetInstance() { return singleMyclass; }
GetInstance() might be called by many threads simultaneously.
Edit:
My question was about the hidden constructs of c++ one must be aware of while using them in multithreaded program. In above case static is not thread safe as compiler adds some instructions for static objects, which is not thread safe. I am looking for similar constructs one should be aware of.
My first answer addressed your example of singleton initialisation, but as you emphasised in an edit to your question, you are after more general pit falls of C++ as we move to multi-core and multi-threaded applications. The following is a real surprise when you first encounter it. Though not C++ specific, it definitely affects C++ code.
Out of order execution and Memory Barriers (or fences):
One gotcha is out of order execution. It is possible for threads to see operations of other threads executing on different cores out of order due to modern hardware allowing out of order execution optimisation. As a result, multi-threaded coded that runs correctly on a single-core machine may in fact be incorrect on a multi-core machine.
A naive solution to such problems is to increase the scope of critical sections. Another is to use memory barriers or lock-free algorithms.