๐Ÿ™‡โ€โ™€๏ธEvent

๊ฐ‘์งˆ ๋ฉ”ํƒ€

์—ฌ๊ธฐ์„œ ์ง์›์€ ์ปค๋„๋ ˆ๋ฒจ์— ์žˆ๋Š” ์ฆ‰, ์•„๋ž˜ ๊ด€๋ฆฌ์ž์— ์žˆ๋Š” ์ง์›์œผ๋กœ ์–ด๋งˆ์–ด๋งˆํ•˜๊ฒŒ ๋Š๋ฆผ

ํ•˜์ง€๋งŒ ์“ฐ๋ ˆ๋“œ ์ž…์žฅ์—์„  ์‹œ๊ฐ„์„ ๋‚ญ๋น„ํ•˜์ง€ ์•Š๊ณ  100ํผ ์ž๋ฌผ์‡ ๊ฐ€ ํ’€๋ฆด๋•Œ ํ™”์žฅ์‹ค์„ ๋“ค์–ด๊ฐ

๐ŸชAutoResetEvent

*ํ†จ๊ฒŒ์ดํŠธ!

  • ๋ฌธ์ด ์ž๋™์œผ๋กœ ์ž ๊ธด๋‹ค
  • ์ฐจ๊ฐ€ ํ•œ๋Œ€์”ฉ ์ง€๋‚˜๊ฐ„๋‹ค
class Lock
{
    // bool <= ์ปค๋„
    AutoResetEvent _available = new AutoResetEvent(true); // ํ†จ๊ฒŒ์ดํŠธ ์ฒซ๋ฌธ์€ ์—ฐ ์ƒํƒœ(true)

    public void Acquire()
    {
        _available.WaitOne(); // ์ž…์žฅ ์‹œ๋„, ์ž๋™์„ ๋ฌธ๋„ ๋‹ซ์•„์คŒ!
        //_available.Reset(); // bool = false // WaitOne์— ํฌํ•จ๋˜์–ด์žˆ๋‹ค
    }

    public void Release()
    {
        _available.Set(); // flag = true
    }
}

AutoResetEvent _available = new AutoResetEvent(true);์€ ์ปค๋„๋‹จ์˜ bool๊ณผ ๊ฐ™๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋จ

_available.WaitOne(); ์— _available.Reset();์ด ํฌํ•จ๋˜์–ด ์žˆ๋‹ค _available.Reset();์€ bool = false ๋ผ๋Š” ๋œป

๐ŸชManualResetEvent

๋ฌธ์ด ์ˆ˜๋™์œผ๋กœ ์ž ๊ธด๋‹ค

๋ฌธ์„ ๋‹ซ์ง€์•Š์œผ๋ฉด ๋ช‡๋Œ€์˜ ์ฐจ๋“  ์ง์›์ด๋“  ์ง€๋‚˜๊ฐ„๋‹ค

class Lock
{
    // bool <= ์ปค๋„
    ManualResetEvent _available = new ManualResetEvent(true);

    public void Acquire()
    {
        _available.WaitOne(); // ์ž…์žฅ ์‹œ๋„
        _available.Reset(); // ๋ฌธ์„ ๋‹ซ๋Š”๋‹ค
    }

    public void Release()
    {
        _available.Set(); // ๋ฌธ์„ ์—ด์–ด์ค€๋‹ค
    }
}

์ด๋ ‡๊ฒŒ ์ฝ”๋“œ๋ฅผ ์งœ๋ฉด ๋ฌธ์„์—ด๊ณ  ๋‹ซ๋Š” ๋ถ€๋ถ„์ด ์›์ž์ ์œผ๋กœ ์ฒ˜๋ฆฌ๊ฐ€ ์•ˆ๋จ

  • ๋งค๋‰ด์–ผ๋ฆฌ์…‹์ด๋ฒคํŠธ๊ฐ€ ํ•„์š”ํ•œ ๋ถ€๋ถ„
    • ๋ชจ๋“  ๋กœ๋”ฉ์ด ๋๋‚˜๊ณ  ์“ฐ๋ ˆ๋“œ๋ฅผ ํ’€์–ด์ฃผ๋Š” ๊ฒฝ์šฐ
      • ManualResetEvent(false);๋กœ ๋ฌธ์„ ๋‹ซ์•„์คŒ
      • ๋กœ๋”ฉ์ด ๋๋‚˜๋ฉด _available.WaitOne(); // ์ž…์žฅ ์‹œ๋„๋ฅผ ํ•œ ์• ๋“ค์ด
      • _available.Set(); // ๋ฌธ์„ ์—ด์–ด์ฃผ๊ณ  ์“ฐ๋ ˆ๋“œ๋“ค์ด ์ผ์„ ์‹œ์ž‘

๐ŸชMutex

namespace ServerCore
{
    class Program
    {
        static int _num = 0;
        static Mutex _lock = new Mutex();

        static void Thread_1()
        {
            for (int i = 0; i < 100000; i++)
            {
                _lock.WaitOne();
                _num++;
                _lock.ReleaseMutex();
            }
        }

        static void Thread_2()
        {
            for (int i = 0; i < 100000; i++)
            {
                _lock.WaitOne();
                _num--;
                _lock.ReleaseMutex();
            }
        }

        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);
        }
    }
}

์ปค๋„ ๋™๊ธฐํ™” ๊ฐœ์ฒด์—ฌ์„œ ๋งค์šฐ ๋Š๋ฆผ

Mutex๋Š” ์ด๋ฒคํŠธ๋ณด๋‹ค ๋” ๋งŽ์€ ์ •๋ณด๋ฅผ ๊ฐ€์ง!

  • ๋ช‡๋ฒˆ ์ž ๊ถœ๋Š”์ง€ ์นด์šดํŒ…
  • ThreadId๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์–ด ์—‰๋šฑํ•œ ์“ฐ๋ ˆ๋“œ๋ฅผ ๋ฆด๋ฆฌ์ฆˆ ํ•˜์ง€ ์•Š๊ฒŒ ํ•จ

AutoResetEvent, ManualResetEvent, Mutext ๋ชจ๋‘ ์ปค๋„๋‹จ์—์„œ ๋™์ž‘!

๊ทธ๋ž˜์„œ ์–ด๋งˆ์–ด๋งˆํ•˜๊ฒŒ ๋Š๋ฆฌ๋‹ค!!

ํƒœ๊ทธ:

์นดํ…Œ๊ณ ๋ฆฌ:

์—…๋ฐ์ดํŠธ: