Concurrency and Parallelism

Concurrency

  • The composition of independently executing tasks.
  • Applied when dealing with handling lots of things at once.
  • The focus is on how to structure a solution to solve a problem which may or may not be solved in a parallel manner.
  • Concurrency is a way to structure a program by breaking it into pieces that can be executed independently.
  • Communication is the means to coordinate the independent executions.
  • Go supports concurrency. Go provides:
    • concurrent execution (goroutines).
    • synchronization and messaging (channels).
    • multi-way concurrent control (select).
  • IMPORTANT POINTS:
    • Concurrency is powerful.
    • Concurrency is not parallelism.
    • Concurrency enables parallelism.
    • Concurrency makes parallelism (and scaling and everything else) easy.

Parallelism

  • Parallelism is the simultaneous execution of computations.
  • Programming as the simultaneous execution of (possibly related) computations.
  • It’s all about doing lots of things at once.

Concurrency vs. parallelism

  • “Concurrency is about dealing with lots of things at once. Parallelism is about doing lots of things at once.” — Rob Pike
    • Concurrency is a property of a program where two or more tasks can be in progress simultaneously. Parallelism is a run-time property where two or more tasks are being executed simultaneously. Through concurrency you want to define a proper structure to your program. Concurrency can use parallelism for getting its job done but remember parallelism is not the ultimate goal of concurrency.
  • Concurrency is about dealing with lots of things at once.
  • Parallelism is about doing lots of things at once.
  • Not the same, but related.
  • Concurrency is about structure, parallelism is about execution.
  • Concurrency provides a way to structure a solution to solve a problem that may (but not necessarily) be parallelizable.
  • An analogy
    • Concurrent: Mouse, keyboard, display, and disk drivers.
    • Parallel: Vector dot product.

Source https://github.com/NanXiao/golang-101-hacks