On Sep 22, 6:32 am, Pavel S <[email protected]> wrote: > Hi > > The problem code is here (line > 00120-00150):http://www.cryptopp.com/docs/ref/misc_8h-source.html > > simple_ptr is not a POD, so when multiple threads calling its > initialization at the same time, some may get not yet constructed > object or the construction may occur multimple times. > > If two threads enter method at the same time, and the construction of > s_pObject is started in both (as static variable flag is not > threadsafe). > For one thread it is finished, and s_pObject.m_p = m_objectFactory(); > is > executed. Then it is finished for second thread and m_p is back NULL. > > Another problem is that once a thread enters retry loop it may never > leave it. As s_objectState is not marked as volatile, the compiler > (MSVC8, Release) optimises out reads of this variable. > > Why actually to initialize "without using locks" ? If the goal is > performance, then double-checked locking should be choosen.
Volatile is fixed in latest version of cryptopp already. s_pObject.m_p being NULL is bad. I thought it would just leak one pointer in worst case... I think main reason not to use locks is to avoid writing whole wrapper around platform specific API for initialization of one singleton. (Although just win32 and pthread should cover most of the platforms cryptopp supports.) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the "Crypto++ Users" Google Group. To unsubscribe, send an email to [email protected]. More information about Crypto++ and this group is available at http://www.cryptopp.com. -~----------~----~----~----~------~----~------~--~---
