Multithreading – I don’t understand multithreading programming
Someone can explain to me how a multithreaded application can be faster when a core CPU can only do one thing at a time If I have 10 threads, only one of these threads is really "running" at any given time on a single core CPU, and all additional threads will only increase the context switching overhead Therefore, if each thread has 10 instructions to process, I still process 100 instructions in order, plus the context switching overhead Did I miss anything here?
Solution
Yes, you missed the fact that a process might block waiting for I / O Therefore, if you use only one thread in your application, it will be very slow if it prevents waiting for I / O to complete
On the other hand, if you have multiple threads, your application may have several waiting for I / O to complete, but the rest "execute", while the OS allows it to access single processor
Remember that I / O operations are orders of magnitude slower than CPU operations
yes. Even in a single core, multithreaded applications can be faster than single threaded applications Consider the case of server processes running on a single thread, such as Apache Whenever there is a connection waiting for I / O to complete, the rest of the connection will stop waiting for I / O to complete Of course, async-io However, the programming model of running a large server like Apache on a single thread using async-io will be too complex to maintain, improve or anything else