What’s the difference between multiprocessing and multithreading?


What’s the difference between multiprocessing and multithreading?

In: 194


A thread is just a single stream of instructions that a programme intends to run. Threads run in a kind of time-share on the same processor. You can run multiple threads on a single processor. You use threads for, for example, one thread drawing the graphics while another handles the physics while another caters for the user’s inputs, etc. That way multiple things can happen at the same time and make full use of the available resources rather than having to wait one after the other. The processor gives each thread a small amount of time to do its thing, then moves on to the next thread, so each thread gets a fair share and everything “progresses” in small snippets of time.

“Multiprocessor” systems actually have entire extra full processors. This used to be called SMP. You would literally have two or more processor slots on the motherboard and two or more main processors. This is still true in servers and high-end configurations

Nowadays the line is blurring as a single physical processor can contain multiple processor cores on the same physical die (e.g. “dual core”) – so it acts like a multiprocessor system, and each of those processors is also capable of handling many threads (Intel sell this as “hyperthreading” in some instances). The same few threads that the programmers request may well be running on entirely different processors, or all on the same one.

In the grand scheme of things, you want as many processor cores as possible, and whether they are on the same processor largely doesn’t matter. Each of those processors you want handling as few threads as possible, but you also need it to be able to handle LOTS of threads – hundreds or more.

And programmers – pretty much don’t care now. They program in threads and the operating system and hardware sorts out what thread runs on what core which is on what chip, and depending on the machine, it makes the best use of the available hardware. That might mean 1000 threads all running on one processor core to save power or because you don’t have extra processors, or 1 thread running on a processor core all on its own to get the entire use of the processor for that one important thread.

But multiprocessors are like owning several apartments. Multithreading is like letting out those apartments on a timeshare. If you have 1 apartment and 50 people sharing it, each person only gets a small amount of time to do what they need to do – so it’s “slow”. If you have 12 apartments (cores) and you have 24 people (threads) sharing them, each one gets a lot more time to do what they need to do, so it’s “faster” and more people (threads) are happier than if they were all sharing the same apartment.

Imagine you’re a pizza baker
* Multithreading: You take one pizza ball, stretch it, top it and put it in the oven. While that pie bakes, you work on your second pizza and so on (you are *concurrently* working on pizzas)

* Multiprocessing: You hire 4 more people, give them all the instructions and you work on your own pizza while they work on theirs (pizzas are being prepared in *parallel*)

The other answers here are inaccurate, and missing one critical aspect — a process has its own memory segment that cannot typically be accessed by other processes. Multiple threads within a process can share the same memory. Multiprocessing needs to coordinate the communication between processes, usually with memory-mapped files or by using sockets. In some cases, like with a debugger, you can access the memory of another process but usually this is out of bounds and you can configure your system to disallow it.

Multiprocessor hardware architectures are another thing altogether. Some of the other answers are confusing that with multiprocessing which is a software topic. You can technically have multiple processes on one physical processor, and it’s multiprocessing if they are working together to do a related task.

For the baker analogy, the recipes are processes, the ovens and mixers are processors. You could have multiple threads for part of the work (e.g. mixing dry ingredients and wet ingredients at the same time before they get combined) and some work needs to be done in serial, like mixing the final batter and then putting it in the oven. This is also assuming the oven can only fit one baked good at a time, or you’d have to think of it as processor = cake sized volume in the oven, or 1 rack in the oven, or similar.

Imagine I’m playing chess with you. My brain is only concerned with this one game. This is one processor running a single thread.

Now imagine one of those grandmasters who play several games simultaneously against different opponents. That’s one processor running multiple threads: a multi-threaded scenario.

Now what of you get two people together in a team? They play against several opponents and they can each move against any of their opponents. This would be several processors working together: a multi-processor environment.

That’s it for the ELI5, but make sure to check out some of the other answers which are a bit more technical as they present interesting bits regarding the evolution of processors, cores, etc.