Multi-Threading

August 14, 2025

5 min read

What is Multithreading?

Well, basically, multithreading is when a program performs multiple operations concurrently.

Let's think about a simple, real life example.

Say you're making dinner.

You could start boiling water.That can be one thread, and you can chop the vegetables at the same time. That's another thread.

In a single-threaded program, you would have to start boiling the water, wait for that process to complete, and then start chopping your vegetables, which would take much longer.

Multithreading within an application works the exact same way.

Each thread can handle one process, and then once all processes are done, you can collect that information and continue with your program.

While this may seem simple at first, there are actually huge problems you can run into with multithreading.

⚠️ Note: Multithreading enables concurrency, but it does not always guarantee parallel execution.

On a single-core CPU, threads take turns running through a mechanism called context switching, which creates the illusion that tasks are happening at the same time.

True parallelism only occurs when multiple CPU cores execute threads simultaneously.

In other words, a program can be multithreaded without actually running tasks in parallel.

•••

En basit tanımıyla multithreading, bir programın birden fazla işlemi aynı anda yürütebilmesi.

Bunu günlük hayattan basit bir örnekle düşün.

Akşam yemeği yapıyorsun.

Suyu kaynatmaya koyarsın - bu bir thread olabilir.
Aynı anda sebzeleri doğramaya başlarsın - bu da başka bir thread.

Eğer programın single-threaded olsaydı, önce suyun kaynamasını beklemen, sonra sebzeleri doğraman gerekirdi. Bu da doğal olarak daha fazla zaman alırdı.

Uygulama içindeki multithreading mantığı da tam olarak böyle çalışır. Her thread bir görevi üstlenir. Tüm işlemler tamamlandığında ise sonuçlar toplanır ve program çalışmaya devam eder.

İlk bakışta oldukça basit görünse de, multithreading beraberinde ciddi problemler getirebilir.

Race Conditions

The Most Common Pitfalls When Writing Multithreaded Code: Race Conditions

Let's say you and your fictional girlfriend are sharing a bank account that has a total of $75 in it.

At 10:00 a.m., you each check the bank account and see it has $75, and you both decide that you are going to make a $50 purchase.

If you both decide to make that purchase at the exact same time, one of them is obviously going to work because there's $75 in the account. But what if the second one also works, leaving your account overdrafted?

This is a classic case of a race condition.

A race condition occurs when multiple threads or processes read and write the same variable - meaning they have access to shared data - and they try to change it at the same time. In such a scenario, threads are “racing” each other to access or modify the data.

⚠️ Note: Race conditions are often hard to detect because they may not happen every time.

They typically appear under high concurrency and can lead to unpredictable and inconsistent system behavior.

Race conditions are not just a multithreading problem, They can also occur in distributed systems, databases, and microservice architectures whenever multiple actors try to modify the same data concurrently.

•••

Multithreaded Kod Yazarken En Yaygın Tuzak: Race Condition

Diyelim ki sen ve arkadaşın ortak bir banka hesabı kullanıyorsun ve hesapta toplam 75 dolar var.

Saat 10:00’da ikiniz de hesabı kontrol ediyorsunuz ve bakiyenin 75 dolar olduğunu görüyorsunuz. Ardından ikiniz de 50 dolarlık bir alışveriş yapmaya karar veriyorsunuz.

Eğer bu alışverişi tam aynı anda yaparsanız, işlemlerden birinin başarılı olması normal çünkü hesapta yeterli para var.
Ama ya ikinci işlem de başarılı olursa? İşte o zaman hesabınız eksi bakiyeye düşer.

Bu durum race condition’ın klasik bir örneği.

Race condition, birden fazla thread veya process aynı veriye erişip onu aynı anda değiştirmeye çalıştığında ortaya çıkar.
Yani, thread’ler veriye erişmek veya onu güncellemek için birbirleriyle yarışır.