### 멀티 쓰레드

private object obj = new object();

lock (obj)
{

}


system.threading.monitor.Enter( obj)
try{

} finally {
	system.threading.Monitor.Exit(obj);
    }

### 사용예 쓰레드 사용하기 1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;


namespace ConsoleApp3
{
    // 다수의 스레드가 하나의 객체를 사용하는 경우
    class ThisLock
    {
        public void IncreaseCount(ref int count)
        {
            count++;
        }
    }

    class Test
    {
        ThisLock lockObject = new ThisLock();
        int Count = 0;

        public void ThreadProc()
        {
            lock (lockObject) // <-- object형으로 처리해도 결과는 동일하다.
            {
                for (int i = 0; i < 10; i++)
                {
                    lockObject.IncreaseCount(ref Count);
                    Console.WriteLine("Thread ID: {0} result: {1}", Thread.CurrentThread.GetHashCode(), Count);
                }
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Test test = new Test();
            Thread[] threads = new Thread[3];
            for (int i = 0; i < 3; i++)
            {
                threads[i] = new Thread(new ThreadStart(test.ThreadProc));
            }

            for (int i = 0; i < 3; i++)
            {
                threads[i].Start();
            }
        }
    }
}

### 모니터 사용하기 lockr과 차이가 없음

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;


namespace ConsoleApp6
{
    // 다수의 스레드가 하나의 객체를 사용하는 경우
    class Test
    {
        int Count;
        object obj = new object();

        public void IncreaseCount()
        {
            Monitor.Enter(obj);
            for (int i = 0; i < 5; i++)
            {
                Count++;
                Console.WriteLine("Thread Id: {0} Count: {1}", Thread.CurrentThread.GetHashCode(), Count);
            }
            Monitor.Exit(obj);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Test test = new Test();
            Thread th1 = new Thread(new ThreadStart(test.IncreaseCount));
            Thread th2 = new Thread(new ThreadStart(test.IncreaseCount));
            th1.Start();
            th2.Start();
        }
    }
}

### lock 같이쓰기?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;


namespace ConsoleApp7
{
    // 다수의 스레드가 하나의 객체를 사용하는 경우
    class ThisLock
    {
        public void IncreaseCount(ref int count)
        {
            count++;
        }
    }

    class Test
    {
        ThisLock lockObject = new ThisLock();
        public int Count = 0;

        public void ThreadProc()
        {
            Monitor.Enter(lockObject);
            for (int i = 0; i < 10; i++)
            {
               lockObject.IncreaseCount(ref Count);
               Console.WriteLine("Thread ID: {0} result: {1}", Thread.CurrentThread.GetHashCode(), Count);
            }
            Monitor.Exit(lockObject);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Test test = new Test();
            Thread[] threads = new Thread[3];
            for (int i = 0; i < 3; i++)
            {
                threads[i] = new Thread(new ThreadStart(test.ThreadProc));
            }

            for (int i = 0; i < 3; i++)
            {
                threads[i].Start();
            }
        }
    }
}

### mutex 쓰레드 사용하기

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;


namespace ConsoleApp7
{
    // 다수의 스레드가 하나의 객체를 사용하는 경우
    class ThisLock
    {
        public void IncreaseCount(ref int count)
        {
            count++;
        }
    }

    class Test
    {
        ThisLock lockObject = new ThisLock();
        public int Count = 0;

        public void ThreadProc()
        {
            Monitor.Enter(lockObject);
            for (int i = 0; i < 10; i++)
            {
               lockObject.IncreaseCount(ref Count);
               Console.WriteLine("Thread ID: {0} result: {1}", Thread.CurrentThread.GetHashCode(), Count);
            }
            Monitor.Exit(lockObject);
        }
    }
    // Mutex를 메서드에 적용한 예
    class Program
    {
        static Mutex mut = new Mutex();
        static int Count;

        static void ThreadProc()
        {
            mut.WaitOne();
            for (int i = 0; i < 5; i++)
            {
                Count++;
                Console.WriteLine("Tread ID: {0} Count: {1}", Thread.CurrentThread.GetHashCode(), Count);

            }
            mut.ReleaseMutex();
        }

        static void Main(string[] args)
        {
            Test test = new Test();
            Thread[] threads = new Thread[3];
            for (int i = 0; i < 3; i++)
            {
                threads[i] = new Thread(new ThreadStart(test.ThreadProc));
            }

            for (int i = 0; i < 3; i++)
            {
                threads[i].Start();
            }
        }
    }
}


### Mutex를 이용한 코드 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;


namespace ConsoleApp9
{
    // 다수의 스레드가 하나의 객체를 사용하는 경우
    class ThisLock
    {
        public void IncreaseCount(ref int count)
        {
            count++;
        }
    }

    class Test
    {
        ThisLock lockObject = new ThisLock();
        Mutex mut = new Mutex();
        public int Count = 0;

        public void ThreadProc()
        {
            mut.WaitOne();
            for (int i = 0; i < 10; i++)
            {
               lockObject.IncreaseCount(ref Count);
               Console.WriteLine("Thread ID: {0} result: {1}", Thread.CurrentThread.GetHashCode(), Count);
            }
            mut.ReleaseMutex();
        }
    }
    // Mutex를 메서드에 적용한 예
    class Program
    {
        static Mutex mut = new Mutex();
        static int Count;

        static void ThreadProc()
        {
            mut.WaitOne();
            for (int i = 0; i < 5; i++)
            {
                Count++;
                Console.WriteLine("Tread ID: {0} Count: {1}", Thread.CurrentThread.GetHashCode(), Count);

            }
            mut.ReleaseMutex();
        }

        static void Main(string[] args)
        {
            Test test = new Test();
            Thread[] threads = new Thread[3];
            for (int i = 0; i < 3; i++)
            {
                threads[i] = new Thread(new ThreadStart(test.ThreadProc));
            }

            for (int i = 0; i < 3; i++)
            {
                threads[i].Start();
            }
        }
    }
}
블로그 이미지

Or71nH

,