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.