Multithreading – What Are Threads and How Do They Work?

multithreading

I'm always confused about thread concepts. I haven't a chance to use them in a real environment so far. It would be helpful if someone could explain threads for me.

Best Answer

A simple explanation would be that you have a job to do, and you get a single person to do the job.

This single person is similar to a thread in a computer.

One person can do one thing at a time, so to do the job, he proceeds through the tasks of the job, one task at a time, one operation on each task at a time.

To speed up a job, you can put more people to the same job. For instance, let's say you need to paint a house. You hire 4 people to do this.

These 4 people could be similar to 4 threads in that they work with the same resources (same house, same buckets of paint) and they can divide up the work.

A process would be akin to the job of painting that house.

This simple explanation somewhat breaks down when it comes to machines that doesn't have enough CPU cores to run all the threads simultaneously, but I'm going to ignore this here.