Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How exactly does multithreading work?

I would like to know how exactly multithreading solves the problem of scalability.My basic understanding is when a request comes in a new thread is alloted to serve the request.But what exactly happens behind the scenes.

I am currently starting a project where I have to build a Storage cloud to handle 1000s of GET,PUT,DELETE operations.Will multithreading address scalability or should I look into evented servers?

like image 430
Sudhagar Avatar asked Aug 31 '25 01:08

Sudhagar


1 Answers

Multithreading lets run more than one thread at once. On a multicore machine, this means two threads can really run in parallel, doing twice the work they'd do running one at a time. Ideally, on a 4 core machine, with 4 threads you'll get almost 4 times as much work done as with a single thread.

For this to work, you need a problem that can be solved with multiple threads running somewhat independently. You'll need to be fairly clever to figure out how to break the program into threads. And, most times, you'll need to be really clever to keep those threads from trashing each other's data (or, worse, subtly sabotaging it). Though it is sometimes possible to run the threads almost independently, like different sessions of the same program, which is nice when you can do it.

Multithreading scales in that you make all the cores work. On a 4 core machine, you get 4 times as much work done--hopefully. If you upgrade to a 16 core machine, you'll get another 4-fold increase--if you did some really clever programming. If you are boasting that you are 50% faster than your competitor, you'd better go with multithreading before your competitor does.

Otherwise, use a single threaded solution. It's a lot simpler. (Thousands does not sound like a lot of data to me. If it could go to millions, multithreading might be worth it.)

like image 57
RalphChapin Avatar answered Sep 03 '25 03:09

RalphChapin