YaqubAliy02 / FileDb.App

1 stars 0 forks source link

Thread safety #4

Open azzimuth opened 6 months ago

azzimuth commented 6 months ago

https://github.com/YaqubAliy02/FileDb.App/blob/043c1ffddd0500cd68199b80e09d0a1ba2f83d3d/FileDb.App/Services/Identities/IdentityService.cs#L19

Your singleton is not thread safe. Add object locking to make it thread safe.

internal sealed class IdentityService : IIdentityService
    {
       ...

        // Lock synchronization object
        private static object locker = new object();

        public static IdentityService GetInstance(IStorageBroker storageBroker)
        {
            // Support multithreaded applications through
            // 'Double checked locking' pattern which (once
            // the instance exists) avoids locking each
            // time the method is invoked
            if (instance == null)
            {
                lock (locker)
                {
                    if (instance == null)
                    {
                        instance =  new IdentityService(storageBroker);
                    }
                }
            }

            return instance;
        }

        ...
    }
YaqubAliy02 commented 6 months ago

Hi, First of all thanks for feedback. Can you provide more explanation about this issue because I could not understand why are you create object. What did you mean by this: lock (locker) please explain.

azzimuth commented 6 months ago

Read this: https://www.dofactory.com/net/singleton-design-pattern