본문 바로가기
서버

SpinLock

by Mostlove 2024. 7. 30.
728x90
반응형

using System;
using System.Threading;
using Systehttp://m.Threading.Tasks;
namespace ServerCore
{
    class SpinLock
    {
        bool _locked = false;
        public void Acquire()
        {
            while(_locked)
            {
                //잠김이 풀리기를 기다린다.

            }
            _locked = true;
        }
        public void Release()
        {
            _locked = false;

        }
    }
    class Program
    {
        static int _num = 0;
        static SpinLock _lock = new SpinLock();
        static void Thread_1()
        {
            for(int i = 0; i < 100000; i++)
            {
                _lock.Acquire();
                _num++;
                _lock.Release();
            }
        }
        static void Thread_2()
        {
            for (int i = 0; i < 100000; i++)
            {
                _lock.Acquire();
                _num--;
                _lock.Release();
            }
        }
        static void Main(string[] args)
        {
            Task t1 = new Task(Thread_1);
            Task t2 = new Task(Thread_2);
            t1.Start();
            t2.Start();
            Task.WaitAll(t1, t2);
        }
    }
}

Deadlock걸림 

Ver.1

using System;
using System.Threading;
using Systehttp://m.Threading.Tasks;
namespace ServerCore
{
    class SpinLock
    {
        volatile int  _locked = 0;
        public void Acquire()
        {
            while (true)
            {
                int original =  Interlocked.Exchange(ref _locked, 1);
                if (original == 0) 
                    break;
            }
        }
        public void Release()
        {
            _locked = 0;

        }
    }
    class Program
    {
        static int _num = 0;
        static SpinLock _lock = new SpinLock();
        static void Thread_1()
        {
            for(int i = 0; i < 100000; i++)
            {
                _lock.Acquire();
                _num++;
                _lock.Release();
            }
        }
        static void Thread_2()
        {
            for (int i = 0; i < 100000; i++)
            {
                _lock.Acquire();
                _num--;
                _lock.Release();
            }
        }
        static void Main(string[] args)
        {
            Task t1 = new Task(Thread_1);
            Task t2 = new Task(Thread_2);
            t1.Start();
            t2.Start();
            Task.WaitAll(t1, t2);
        }
    }
}

Ver.2

using System;
using System.Threading;
using Systehttp://m.Threading.Tasks;
namespace ServerCore
{
    class SpinLock
    {
        volatile int  _locked = 0;
        public void Acquire()
        {
            int expected = 0;
            int desired = 1;
            while (true)
            {
                /*int original =  Interlocked.Exchange(ref _locked, 1);
                if (original == 0) 
                    break;*/

                //CAS Compare-And-Swap
                if(Interlocked.CompareExchange(ref _locked, desired, expected)==expected)
                    break;
            }
        }
        public void Release()
        {
            _locked = 0;

        }
    }
    class Program
    {
        static int _num = 0;
        static SpinLock _lock = new SpinLock();
        static void Thread_1()
        {
            for(int i = 0; i < 1000000; i++)
            {
                _lock.Acquire();
                _num++;
                _lock.Release();
            }
        }
        static void Thread_2()
        {
            for (int i = 0; i < 1000000; i++)
            {
                _lock.Acquire();
                _num--;
                _lock.Release();
            }
        }
        static void Main(string[] args)
        {
            Task t1 = new Task(Thread_1);
            Task t2 = new Task(Thread_2);
            t1.Start();
            t2.Start();
            Task.WaitAll(t1, t2);
            Console.WriteLine(_num);
        }
    }
}

반응형

'서버' 카테고리의 다른 글

AutoResetEvent  (0) 2024.08.06
Context Switching  (0) 2024.08.06
DeadLock  (0) 2024.07.30
Lock 기초  (0) 2024.07.30
메모리 배리어  (1) 2024.07.26