> template <class T, class F, int instance>
> const T & Singleton<T, F, instance>::Ref(CRYPTOPP_NOINLINE_DOTDOTDOT)
> const
> {
> static simple_ptr<T> s_pObject;
> static volatile char s_objectState = 0; // [*]
>
> retry:
> switch (s_objectState)
> {
> case 0:
> s_objectState = 1; // [1]
> try
> {
> s_pObject.m_p = m_objectFactory();
> }
> catch(...)
> {
> s_objectState = 0;
> throw;
> }
> s_objectState = 2; // [2]
> break;
> case 1:
> goto retry; // [3]
> default:
> break;
> }
> return *s_pObject.m_p;
>
> }
Actually this code still has problems with multithreading. Following
operation must be atomic: (if s_objectState == 0 then s_objectState =
1), and there is no way to guarantee that. 2 threads can enter segment
[1]-[2] if one loses execution time between "switch (s_objectState)"
and [1]. I'm not sure it is possible to make this thread-safe without
actual lock. (might be possible in C++0x with new atomic<> stuff, but
not yet)
--~--~---------~--~----~------------~-------~--~----~
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.
-~----------~----~----~----~------~----~------~--~---