private static CryptoRandom m_pInstance = null;
public static CryptoRandom Instance
{
get
{
if(m_pInstance != null) return m_pInstance;
m_pInstance = new CryptoRandom();
return m_pInstance;
}
}But it's a "No" because the authors are lucky in this case - not because they are competent.
Now, that's just one instance of poor skill. There are many more. Are you sure none of them have security implications?
public ulong GeneratedBytesCount
{
get
{
ulong u;
lock(m_oSyncRoot) { u = m_uGeneratedBytesCount; }
return u;
}
}
...so if you care about thread safety at some point in your class, then you should care about it during it's initialisation.And in all likelihood it is thread-safe - but that's due to being lucky - not competent.
The larger issue is that we have a widely-used crypto software which is clearly (1) not designed well; (2) not implemented well.
How much trust one is willing to place into current and future versions by the same author(s) is up to you.
But "(2) the authors clearly intended this library to be thread-safe" means that piece of code is bad. So you have a point here.
But since it's a singleton, having a non-thread-safe initializer is cutting that distinction a little fine.
I'm sure the author would be very happy to see a sudden influx of contributions to the project, and we'd all have a better product in the end too.
Seems odd the spirit of open source in this respect tends to be more about pointing out the failures of the author than to collectively improve the actual product.
I do agree that technically you are correct and you should wear belt and suspenders, especially if it's a library for third-party consumption and labeled as thread-safe... but still... its pretty esoteric, and only used by the author (I assume) who knows its not thread-safe. Locking isn't exactly without its performance implications either and even though that is neglible it feels unecessary if a race condition is de facto near impossible.
http://csharpindepth.com/Articles/General/Singleton.aspx
Note that using the last example isn't necessarily "the best", it really depends on your requirements.
Nonetheless, a very interesting read.
Most UI applications sooner or later need at least some basic background processing but if I was writing a simple password manager, I'd most likely just do everything blocking on the UI thread.
That said: for simple patterns like singleton, there is really no reason not to use the builtin and recommended way which is the Lazy<T>.