Home > Software engineering >  Multithreading and Parallellism definitions
Multithreading and Parallellism definitions

Time:11-11

I've started studying multithreading in C and I'm trying to understand how it works, but on the internet I can't find a clear explanation. I have wrote this code:

#include <iostream>
#include <thread> 
using namespace std; 


void thread_one() 
{ 
    while(true)
    {
        std::cout << " - " << std::flush;
        std::this_thread::sleep_for(1000ms);
    }
} 

void thread_two() 
{ 
    while(true)
    {
        std::cout << " * " << std::flush;
        std::this_thread::sleep_for(1000ms);
    }
} 

int main() 
{ 

    thread th1(thread_one); 
    thread th2(thread_two); 

    th1.join(); 
    th2.join();
} 

which will give me something like : * - - - * * * - * - , therefore they are clearly running at the same time.

Now from the definitions that I found on the internet I can't get if this program is running in parallel or if it's a multithreading application, I can't get the difference. From what I've understood : with multithreading the computer can run (if available) on more than one core, therefore doing more than one tasks at the same time (Concurrently ?) parallellism is when a single core is doing 2 different things concurrently.

But still I can't get if doing a code like this is multithreading or parallellism and if my understanding are correct or not.

Can someone please explain to me the exact difference? thanks in advance

CodePudding user response:

"Parallelism" is something entirely different from "concurrency".

You are mixing up "multi processing" with "multi threading" on the OS side (this introduces concurrency), and "multi CPU" vs "multi-core" vs "SMT" on the hardware end (this introduces parallelism).

"Parallelism" says "you are actually doing something in parallel". You actually need 2 or more instances of any resource to achieve this.

"Concurrency" says primarily "you are going to have conflicts on shared resources".

As it happens, you do have 2 threads which are running concurrently, and both are sharing your terminal as a common resource.

As both of them hang mostly in sleeps, they didn't actually ran in "parallel" though. Your operating systems scheduler could have very well scheduled them both to the same CPU core, sequentially, and you wouldn't have noticed.

Scheduling is the keyword here, as soon as you try to run more tasks concurrently than you have resources for, the scheduler starts arbitration of the resources. If you were to do this with more threads than you CPU cores for, and you didn't used sleeps either, you would still see the outputs from all threads interleaved, as the scheduler will use eviction as a strategy for fair scheduling, even in the absence of an voluntary yield.

CodePudding user response:

Computer science-y definition of concurrency: Two program operations are concurrent with each other if the program does not guarantee that they always will be performed one after another, and always in the same order.

Informal definition that often amounts to the same thing: Two activities/function-calls/processes/threads/etc. are concurrent with each other if there is any moment in time when both of them have been started, but neither one of them has finished.

Parallel computation: happens when a single computation is able to exploit multiple instances of some kind of computing hardware. Besides using multiple processes or threads on a single symmetric multiprocessing computer, or on a cluster of computers; there are/have been other, more esoteric computer architectures meant for parallel computing such as; pipelined graphics processors, vector processors, systolic array processors, and reconfigurable processors.

  • Related