
Fabien NiƱoles wrote:
In a multithreaded app this might work on some platforms, on others it doesn't:
Quote from http://msdn2.microsoft.com/en-us/library/s1sb61xd.aspx: "Assigning to a static local variable is not thread safe and is not recommended as a programming practice."
Although this isn't very concise wording (IIRC, the standard says that it should work for literals assigned to PODs) it seems clear that it won't work for non-PODs...
For assignation, sure, but I don't see any in my code...
As I said the wording isn't very concise. I think it's obvious that it will not work if state_dispatcher has a non-trivial ctor (which I assume it has).
However, if that's bother, I think then only something like Loki Singleton pattern will do. You then need a double lock for accessing it correctly, something like this:
static state_dispatcher const& get() { static state_dispatcher* pInstance; scoped_lock first_lock(m_mutex1) if (!pInstance) { scoped_lock second_lock(m_mutex2); if (!pInstance) { pInstance = new state_dispatcher; } } return *pInstance; }
From where did you get m_mutex1 & m_mutex2?? Where are they initialized?
-- Andreas Huber When replying by private email, please remove the words spam and trap from the address shown in the header.