seegongsik
Operating systems

What is a thread

One job can be split into several strands that run at the same time in the same workshop. Those strands are threads.

01

One person, many hands

A cook works in the kitchen, boiling soup while chopping vegetables and keeping an eye on the flame now and then.
There's one job, cooking, but within it several strands are handled together. There's one kitchen and one pantry, and they're shared.
A computer can likewise split the work within one job into several strands, and each of those strands is called a thread.
With the work split into strands, you don't sit idle while waiting for the soup to boil; you can tend to another strand.

Task (cooking)CookSimmer soupChop vegWatch flameShelf (shared)

One job, many strands inside it. The workshop and ingredients are shared.

Threads are the several strands that run at the same time within one job.

02

Two separate houses, many rooms in one house

Earlier we said each process has its own separate space. Two processes are like neighbors with a wall between them; they can't just walk into each other's homes.
But threads within one process are like a family living in one house. The rooms are separate, but the living room and fridge are shared.
That's why threads can easily exchange information: they're looking at the same pantry. For processes, that's more of a hassle.
The wall may look like a nuisance, but it's also a welcome barrier that keeps your house dry even if the neighbor's pipes leak.

Processes: a wallThreads: sharedstrangerssharedone familyliving room and fridge shared

Processes are neighbors behind a wall; threads are family in one house.

Processes use separate spaces, while the threads within one process share a space.

03

Adding more strands

Change how many strands one job is split into.
With one strand, the work gets done one piece at a time. Add strands, and several parts move forward at once, all looking at the same pantry.
More strands can finish the job sooner. But since everyone uses the same kitchen, too many of them get in each other's way.
Even if you double the strands, the job doesn't get exactly twice as fast, because more time goes to stepping aside for each other in the cramped kitchen.

Shelf (shared)
Strand 1
Strand 2
Strands2
Add strands and several parts move at once. But there is one shelf, so they share it.

Change the number of strands to see how one job is divided and carried out.

More strands means more progress at once, but they still share the same space.

04

Sharing is fast, but

Threads sharing the pantry is a big advantage. There's no need to move or copy things separately; they look at the same thing directly, so it's light and fast.
But there's another side. If two strands touch the same ingredient at the same moment, they can collide, like one adding salt while the other adds it too.
So sharing is convenient, but you have to be careful about who touches what, and when. This story continues a little later, in the race condition lesson.
Still, sharing is the choice because giving each strand its own copy of the pantry would be heavier and slower.

UpsideShelfSeeing the same is light and fasttwo sidesCareTouching at once collides

Sharing is light and fast, but touching the same thing at once causes collisions.

Sharing a space is fast and light, but touching the same thing at the same time can cause problems.

05

Summing it up on one page

There are just three things to remember.
First, threads are the several strands within one job: there's one job, but several parts move forward at once inside it.
Second, they share the same space. Processes have walls between them, but the threads within one process look at the same pantry. Third, that makes them light and fast but calls for care, because touching the same thing at once can cause collisions.
Know these three, and you can see why some apps keep responding without freezing: one strand keeps the screen moving while another quietly works in the background.

One
Threads are several strands inside one task. The job is one, but several parts move at once.
Two
They share the same space. Processes have a wall, threads look at the same shelf.
Three
Light and fast but needs care. Touching the same thing at once can collide.

Many strands in one job, a shared space, light but needing care.

In one lineThreads split one job into several strands that move forward together in the same space.
← Operating systems
Was this helpful? Support seegongsik