Get rid of lock(this)

- Use readonly object as lock

Info from stackoverflow:

If I want to be sure that it will be locked for all threads inside my
application:
The lock object has to be static, if it locks access to static state.
Otherwise it has to be instance, because there's no need to lock state of
one class instance, and prevent other threads to work with another class
instance at the same time.

everyone says that the object has to be "readonly" I didn't found the
reason
Well, it doesn't have to be. This is just a best practice, which helps you
to avoid errors.

Signed-off-by: Syrone Wong <wong.syrone@gmail.com>
This commit is contained in:
Syrone Wong
2016-09-29 16:39:58 +08:00
parent d5f24df128
commit 46d95e7411
4 changed files with 18 additions and 7 deletions
@@ -15,6 +15,9 @@ namespace Shadowsocks.Encryption
private IntPtr _encryptCtx = IntPtr.Zero;
private IntPtr _decryptCtx = IntPtr.Zero;
// instance based lock
private readonly object _Lock = new object();
public MbedTLSEncryptor(string method, string password, bool onetimeauth, bool isudp)
: base(method, password, onetimeauth, isudp)
{
@@ -120,7 +123,7 @@ namespace Shadowsocks.Encryption
protected virtual void Dispose(bool disposing)
{
lock (this)
lock (_Lock)
{
if (_disposed)
{