C#
Özet - Thread ve ThreadPool
Thread, lock ve Interlocked'ı birleştirerek bir sayaç yazarsın.
Özet - Thread ve ThreadPool
Bu grupta Thread sınıfını, Start/Join ile senkronizasyonu, ThreadPool'u, lock deyimini ve Interlocked'ı öğrendin.
Bunlar düşük seviye paralel programlamanın temelidir. Modern C#'ta çoğu durumda Task/async tercih edilir ama Thread, lock ve Interlocked hâlâ önemli araçlardır. Şimdi iki sayaç örneği yazalım.
Uygulama lock ile 5'er kez artır
private static readonly object _sync = new object(); ve private static int _counter = 0; tanımla. Main'de 2 thread oluştur, her biri for (int i = 0; i < 5; i++) { lock (_sync) { _counter++; } } çağırsın. Thread'leri Join ile bekle. Console.WriteLine($"Sayaç: {_counter}"); yaz. (2 thread * 5 artırma = 10 beklenir.)
Örnek çözümü göster
Thread t1 = new Thread(() => { for (int i = 0; i < 5; i++) { lock (_sync) { _counter++; } } }); Thread t2 = new Thread(() => { for (int i = 0; i < 5; i++) { lock (_sync) { _counter++; } } }); t1.Start(); t2.Start(); t1.Join(); t2.Join(); Console.WriteLine($"Sayaç: {_counter}");
Örnek çözüm:
using System;
using System.Threading;
class Program
{
private static readonly object _sync = new object();
private static int _counter = 0;
static void Main()
{
Thread t1 = new Thread(() => {
for (int i = 0; i < 5; i++) { lock (_sync) { _counter++; } }
});
Thread t2 = new Thread(() => {
for (int i = 0; i < 5; i++) { lock (_sync) { _counter++; } }
});
t1.Start();
t2.Start();
t1.Join();
t2.Join();
Console.WriteLine($"Sayaç: {_counter}");
}
}
ThreadPool ve Interlocked birlikte
ThreadPool kuyruğa eklediğin işlerde paylaşılan bir sayaç tutuyorsan Interlocked kullanmak en temiz çözümdür. Lock'a göre çok daha hızlıdır; her iş kendi thread'inde çalışır ama Interlocked atomikliği garanti eder.
Bu desen, örneğin web sunucusunda istek sayacı tutmak için uygundur. Kod hem güvenli hem de yüksek performanslı olur.
Uygulama Interlocked ile 3'er kez artır
private static int _counter = 0; tanımla. Main'de 2 thread oluştur, her biri for (int i = 0; i < 3; i++) { Interlocked.Increment(ref _counter); } çağırsın. Thread'leri Join ile bekle. Console.WriteLine($"Sayaç: {_counter}"); yaz. (2 thread * 3 artırma = 6 beklenir.)
Örnek çözümü göster
Thread t1 = new Thread(() => { for (int i = 0; i < 3; i++) { Interlocked.Increment(ref _counter); } }); Thread t2 = new Thread(() => { for (int i = 0; i < 3; i++) { Interlocked.Increment(ref _counter); } }); t1.Start(); t2.Start(); t1.Join(); t2.Join(); Console.WriteLine($"Sayaç: {_counter}");
Örnek çözüm:
using System;
using System.Threading;
class Program
{
private static int _counter = 0;
static void Main()
{
Thread t1 = new Thread(() => {
for (int i = 0; i < 3; i++) { Interlocked.Increment(ref _counter); }
});
Thread t2 = new Thread(() => {
for (int i = 0; i < 3; i++) { Interlocked.Increment(ref _counter); }
});
t1.Start();
t2.Start();
t1.Join();
t2.Join();
Console.WriteLine($"Sayaç: {_counter}");
}
}
Kendini test et
-
Hangisi lock olmadan atomik artırma sağlar?
- counter++
- Interlocked.Increment
- lock (counter)
- Thread.Sleep
Cevabı göster
Interlocked.Increment — Doğru. Interlocked.Increment lock'suz atomik artırma yapar.
-
Kavramları eşleştir
- Thread
- Ayrı iş parçacığı
- ThreadPool
- Hazır havuz
- lock
- Kritik bölge
- Interlocked
- Atomik işlem
Sıradaki ders: ConcurrentDictionary