Threading vs Parallelism, how do they differ?

前端 未结 9 2121
后悔当初
后悔当初 2020-12-04 07:15

What is the difference between threading and parallelism?

Which one has advantage over the other?

相关标签:
9条回答
  • 2020-12-04 07:48

    If we think CPU as a company and threads as its workers then, it help us to understand threading and parallelism more easily.

    Like a company have many workers, the CPU also have many threads.

    Also there may be more than one company and therefore there may be more than one CPU's.

    Therefore when workers(threads) work in a company(CPU), it is called threading.

    And when two or more companies(CPU) work independently or together, it is called parallelism.

    0 讨论(0)
  • 2020-12-04 07:54

    Threading is a poor man's parallelism.

    EDIT: To be more precise:

    Threading has nothing to do with parallelism and wise versa. Threading is about making feel that some processes run in parallel. However, this doesn't make processes to complete ALL their actions any faster in total.

    0 讨论(0)
  • 2020-12-04 07:56

    Daniel Moth (a former coworker of mine)- Threading/Concurrency vs Parallelism article explains it all.

    Quoted:

    To take advantage of multiple cores from our software, ultimately threads have to be used. Because of this fact, some developers fall in the trap of equating multithreading to parallelism. That is not accurate...You can have multithreading on a single core machine, but you can only have parallelism on a multi core machine

    The quick test: If on a single core machine you are using threads and it makes perfect sense for your scenario, then you are not "doing parallelism", you are just doing multithreading.

    0 讨论(0)
  • 2020-12-04 07:58

    How do you define "parallelism"? Multithreading is a concrete implementation of the concept of parallel program execution.

    The article RichardOD linked to seems to be mainly concerned with whether threads are actually executed in parallel on a concrete machine.

    However, your question seems to see multithreading and parallelism as opposites. Do you perhaps mean programs that use multiple processes rather than multiple threads? If so, the differences are:

    • Threads are much cheaper to create than processes. This is why using threads rather than processes resulted in a huge speedup in web applications - this was called "FastCGI".
    • Multiple threads on the same machine have access to shared memory. This makes communication between threads much easier, but also very dangerous (it's easy to create bugs like race conditions that are very hard to diagnose and fix).
    0 讨论(0)
  • 2020-12-04 08:01

    There are two different kinds of concurrency:

    1. Threading: CPU switches between different threads really fast, giving a falsehood of concurrency. Keypoint: only one thread is running at any given time. When one thread is running, others are blocked. You might think, how is this any useful than just running procedurally? Well, think of it as a priority queue. Threads can be scheduled. CPU scheduler can give each thread a certain amount of time to run, pause them, pass data to other threads, then give them different priorities to run at a later time. It's a must for not instant running processes that interact with each other. It's used in servers extensively: thousands of clients can request something at the same time, then getting what they requested at a later time (If done procedurally, only one client can be served at a time). Philosophy: do different things together. It doesn't reduce the total time (moot point for server, because one client doesn't care other clients' total requests).
    2. Parallelism: threads are running parallel, usually in different CPU core, true concurrency. Keypoint: mlutiple threads are running at any given time. It's useful for heavy computations, super long running processes. Same thing with a fleet of single core machines, split data into sections for each machine to compute, pool them together at the end. Different machines/cores are hard to interact with each other. Philosophy: do one thing in less time.

    As you can see, they solve totally different kinds of problems.

    0 讨论(0)
  • 2020-12-04 08:01

    Threading is a technology, parallelism is a paradigm that may be implemented using threading (but could just as easily be done using single threads on multiple processors)

    0 讨论(0)
提交回复
热议问题