Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Misunderstanding the difference between single-threading and multi-threading programming

I have a misunderstanding of the difference between single-threading and multi-threading programming, so I want an answer to the following question to make everything clear.

Suppose that there are 9 independent tasks and I want to accomplish them with a single-threaded program and a multi-threaded program. Basically it will be something like this:

Single-thread:

- Execute task 1 - Execute task 2 - Execute task 3 - Execute task 4 - Execute task 5 - Execute task 6 - Execute task 7 - Execute task 8 - Execute task 9 

Multi-threaded:

Thread1:

- Execute task 1 - Execute task 2 - Execute task 3 

Thread2:

- Execute task 4 - Execute task 5 - Execute task 6 

Thread3:

- Execute task 7 - Execute task 8 - Execute task 9 

As I understand, only ONE thread will be executed at a time (get the CPU), and once the quantum is finished, the thread scheduler will give the CPU time to another thread.

So, which program will be finished earlier? Is it the multi-threaded program (logically)? or is it the single-thread program (since the multi-threading has a lot of context-switching which takes some time)? and why? I need a good explanation please :)

like image 982
Eng.Fouad Avatar asked Apr 11 '12 17:04

Eng.Fouad


People also ask

What is the difference between single threading and multi threading?

"Single-threaded" means that we open a single connection and measure the speeds from that. "Multi-threaded" means that we're using multiple connections - usually anywhere from 3 to 8 - at the same time, and measure the total speed across them all.

What are threads differentiate between a single threaded process and a multi-threaded process?

In single-threaded processes, the process contains one thread. The process and the thread are one and the same, and there is only one thing happening. In multithreaded processes, the process contains more than one thread, and the process is accomplishing a number of things at the same time.

What is the difference between single threading and multi threading What are the advantages of the operating system that use multi threading?

Single thread refers to executing an entire process from beginning to end without interruption by a thread while multi-thread refers to allowing multiple threads within a process so that they are executed independently while sharing their resources.

What are the differences between threads and multi process?

By formal definition, multithreading refers to the ability of a processor to execute multiple threads concurrently, where each thread runs a process. Whereas multiprocessing refers to the ability of a system to run multiple processors concurrently, where each processor can run one or more threads.


1 Answers

It depends.

How many CPUs do you have? How much I/O is involved in your tasks?

  1. If you have only 1 CPU, and the tasks have no blocking I/O, then the single threaded will finish equal to or faster than multi-threaded, as there is overhead to switching threads.

  2. If you have 1 CPU, but the tasks involve a lot of blocking I/O, you might see a speedup by using threading, assuming work can be done when I/O is in progress.

  3. If you have multiple cpus, then you should see a speedup with the multi-threaded implementation over the single-threaded since more than 1 thread can execute in parallel. Unless of course the tasks are I/O dominated, in which case the limiting factor is your device speed, not CPU power.

like image 185
hvgotcodes Avatar answered Oct 03 '22 11:10

hvgotcodes