DEV Community

Venkatesh bellale
Venkatesh bellale

Posted on

Explain CPU Threads Like I'm Five

Top comments (1)

Collapse
 
cjbrooks12 profile image
Casey Brooks

Imagine you and your friends want to build a sandcastle. When you started, the sandcastle was kinda small, and only one of you could get close enough to it at a time to build it. But as the sandcastle got bigger, you started to notice that progress was slow and your friends were getting bored because they were just sitting around doing nothing. Even though the sandcastle was now big enough for several of your friends to build it at once, you still only allowed one to build on it at a time.

Being the smart 5-year-old you are, you suggest that your friends come and help build at the same time as you. But now you have a different problem. Even though progress is going much, much faster now that you've got multiple kids building concurrently, they tend to get in each other's way. You're building a tower, but as your friend goes to attach his wall to the tower, you accidentally knock his elbow. Whoops! Both the tower and the wall start to crumble. Clearly, this isn't going to solve the problem entirely.

But you still want to build it fast, and build with your friends, so you start drawing some lines around the castle, breaking the area around it up into little sections. You tell each of your friends that they can do whatever they need to inside their own section, but they must never cross into another section without permission. When one friend needs to build across sections, you will have both friends step away, you'll erase the line between the two sections, and you will decide which friend will then connect the two sections of the sandcastle together. In this way, we are still building the sandcastle quickly (although there is a bit of extra work involved in drawing and erasing lines), but we're not knocking into one another.