[C#] μ€λ λ©
C#μμ μ€λ λ©μ μ¬μ©λ²μ κ°λ¨νλ€.
Thread ν΄λμ€μ ν¨μλ₯Ό μΈμλ‘ μ€μ μμ±νκ³ ,
Start()λ©μλλ₯Ό νΈμΆνλ©΄ λλ€.
μ¬κΈ°μμ ν¨μλ κ°μ²΄μ λ©μλλ μ μ λ©μλ, λλ€μμ΄ λ€ λ€μ΄κ° μ μλ€.
λμ ν¨μμ νμμ void(void)μ¬μΌλ§ νλ€. 리ν΄μ΄λ νλΌλ―Έν°κ° μμΌλ©΄ μλλ€.
μλλ μ€ν μμλ€.
using System;
using System.Threading;
using System.Collections.Generic;
namespace ThreadingTest
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("main thread μμ");
var threads = new List<Thread>();
threads.Add(new Thread(() => Console.WriteLine("첫λ²μ§Έ thread")));
threads.Add(new Thread(() => Console.WriteLine("λλ²μ§Έ thread")));
threads.Add(new Thread(() => Console.WriteLine("μΈλ²μ§Έ thread")));
threads.Add(new Thread(() => Console.WriteLine("λ€λ²μ§Έ thread")));
//μ λΆ λλ¦Ό
foreach (var thread in threads)
thread.Start();
Console.WriteLine("main thread μ’
λ£");
}
}
}
μ λμκ°λ€.
μμκ° μ λ©λλ‘μΈκ±΄ μ΄μ©μμλ μ€λ λ©μ μλͺ
μ΄λ€.
μ€λ λκ° λ€ λλ λκΉμ§ mainμ λκΈ°μν€λ €λ©΄ joinμ μ°λ©΄ λλ€.

κ·Έλ¦¬κ³ λ°μ΄ν° λ μ΄μ€λΌλ κ²μ΄ μλ€.
μ¬λ¬ μ€λ λμμ νλμ λ³μλ₯Ό 곡μ ν λ λ°μνλ λ¬Έμ μΈλ°, μμ λ₯Ό ν΅ν΄ νλ² λ΄λ³΄μ
νλμ λ³μλ₯Ό μ¬λ¬ μ€λ λμμ λμμ μ¦κ°μν€λ μ½λλ€.
using System;
using System.Threading;
using System.Collections.Generic;
namespace ThreadingTest
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("main thread μμ");
var threads = new List<Thread>();
int counter = 0;
//10000μ© μ¦κ°μν΄
ThreadStart inc = () => {
for (int i = 0; i < 10000; i++)
counter++;
};
threads.Add(new Thread(inc));
threads.Add(new Thread(inc));
threads.Add(new Thread(inc));
threads.Add(new Thread(inc));
//μ λΆ λλ¦Ό
foreach (var thread in threads)
thread.Start();
//μ΄λλ€ λλ λκΉμ§ main λΈλ
foreach (var thread in threads)
thread.Join();
Console.WriteLine($"{counter}");
Console.WriteLine("main thread μ’
λ£");
}
}
}
λΆλͺ
40000μ΄ λμμΌνλλ° κ·Έκ±°λ³΄λ€ λλμλ€.
κ°μ μ¬λ¦¬λ μμ€μ μ€λ λλΌλ¦¬ μΆ©λμ ν΄μ λ―Έμ€κ° λ κ²μΈλ°, μ΄κ±Έ ν΄κ²°νλ €λ©΄ Mutexλ‘ μκ³κ΅¬μμ μ€μ ν΄μ£Όκ±°λ Atomic λ±μΌλ‘ λκΈ°νλ₯Ό ν΄μ€μΌ νλ€.
μλλ λλ΅μ μΈ mutexμ μ¬μ©λ²μ΄λ€.
using System;
using System.Threading;
using System.Collections.Generic;
namespace ThreadingTest
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("main thread μμ");
var threads = new List<Thread>();
int counter = 0;
var locker = new Mutex();
//10000μ© μ¦κ°μν΄
ThreadStart inc = () => {
locker.WaitOne(); //νλ ν μ κΈ
for (int i = 0; i < 10000; i++)
counter++;
locker.ReleaseMutex(); //μ κΈ ν΄μ
};
threads.Add(new Thread(inc));
threads.Add(new Thread(inc));
threads.Add(new Thread(inc));
threads.Add(new Thread(inc));
//μ λΆ λλ¦Ό
foreach (var thread in threads)
thread.Start();
//μ΄λλ€ λλ λκΉμ§ main λΈλ
foreach (var thread in threads)
thread.Join();
Console.WriteLine($"{counter}");
Console.WriteLine("main thread μ’
λ£");
}
}
}
μ λλ€.
κ·Έλ¦¬κ³ Monitorλ λλ μλ€.
νλμ νλ‘μΈμ€λ₯Ό μ¬μ©ν λλ§ μ ν©νλ€. νμ§λ§ Mutexλ³΄λ€ μμλ°°λ λΉ λ₯΄λ€κ³ νλ€.
using System;
using System.Threading;
using System.Collections.Generic;
namespace ThreadingTest
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("main thread μμ");
var threads = new List<Thread>();
int counter = 0;
var locker = new Object();
//10000μ© μ¦κ°μν΄
ThreadStart inc = () => {
Monitor.Enter(locker); //μ κΈ
for (int i = 0; i < 10000; i++)
counter++;
Monitor.Exit(locker); //μ κΈ ν΄μ
};
threads.Add(new Thread(inc));
threads.Add(new Thread(inc));
threads.Add(new Thread(inc));
threads.Add(new Thread(inc));
//μ λΆ λλ¦Ό
foreach (var thread in threads)
thread.Start();
//μ΄λλ€ λλ λκΉμ§ main λΈλ
foreach (var thread in threads)
thread.Join();
Console.WriteLine($"μΉ΄μ΄ν°λ {counter}");
Console.WriteLine("main thread μ’
λ£");
}
}
}
μ λλ€.
κ·Έλ¦¬κ³ C#μμλ μ΄κ±Έ λ¬Έλ²μΌλ‘λ μ 곡νλ€.
λ°λ‘ 곡μ ν΄μ μ κΈ μΈμμ©μΌλ‘ μΈ κ°μ²΄λ₯Ό λ§λ€κ³ lock λΈλμΌλ‘ μ κΈμ κ±°λκ²μ΄λ€.
using System;
using System.Threading;
using System.Collections.Generic;
namespace ThreadingTest
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("main thread μμ");
var threads = new List<Thread>();
int counter = 0;
var locker = new Object(); //μ κΈμ© κ°μ²΄
//10000μ© μ¦κ°μν΄
ThreadStart inc = () => {
lock (locker) //λ€μ΄κ°λ©΄ μ κ°λ²λ¦Ό
{
for (int i = 0; i < 10000; i++)
counter++;
}
};
threads.Add(new Thread(inc));
threads.Add(new Thread(inc));
threads.Add(new Thread(inc));
threads.Add(new Thread(inc));
//μ λΆ λλ¦Ό
foreach (var thread in threads)
thread.Start();
//μ΄λλ€ λλ λκΉμ§ main λΈλ
foreach (var thread in threads)
thread.Join();
Console.WriteLine($"μΉ΄μ΄ν°λ {counter}");
Console.WriteLine("main thread μ’
λ£");
}
}
}
κ·Έλ°λ° μ΄λ° μκ³κ΅¬μμ μ€μ μ μ±λ₯μ μμ²λκ² μ‘μλ¨ΉκΈ° λλ¬Έμ, μ΅μνμΌλ‘ μ¬μ©νλ κ²μ΄ μ’λ€.
κ·Έλ¦¬κ³ Atomic, κ·Έλ¬λκΉ μμμ μ°μ°μ νμ©ν μλ μλ€.
using System;
using System.Threading;
using System.Collections.Generic;
namespace ThreadingTest
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("main thread μμ");
var threads = new List<Thread>();
int counter = 0;
//10000μ© μ¦κ°μν΄
ThreadStart inc = () => {
for (int i = 0; i < 10000; i++)
Interlocked.Increment(ref counter); //atomic μ°μ°
};
threads.Add(new Thread(inc));
threads.Add(new Thread(inc));
threads.Add(new Thread(inc));
threads.Add(new Thread(inc));
//μ λΆ λλ¦Ό
foreach (var thread in threads)
thread.Start();
//μ΄λλ€ λλ λκΉμ§ main λΈλ
foreach (var thread in threads)
thread.Join();
Console.WriteLine($"μΉ΄μ΄ν°λ {counter}");
Console.WriteLine("main thread μ’
λ£");
}
}
}
μ΄κ²λ μ λλ€.
λμ²΄λ‘ Mutexλ₯Ό ν΅ν λΈλ보λ€λ μμμ μ°μ°μ νμ©νλ κ²μ΄ μ±λ₯μ λ μ’λ€κ³ λ€ νλ€.