Advanced Golang

Concurrency

Sequential vs Concurrent Processing

In this article, we explore the key differences between sequential and concurrent programming. Understanding these concepts is crucial for grasping how concurrency works in Golang and other modern programming languages.

Sequential Programming

Sequential programming executes instructions one after another in a strict linear order. Each operation must finish before the next one begins. For instance, consider a simple program that calculates the sum of two numbers. The program might follow these steps:

  1. Request the first number from the user.
  2. Request the second number from the user.
  3. Compute the sum of the two numbers.
  4. Return the result.

This fixed, step-by-step order is typical for basic, independent operations.

The image illustrates a sequential processing flow on a CPU core, showing steps to get two numbers, calculate their sum, and return the result.

Multitasking

Modern CPUs enhance sequential execution by employing multitasking. Even on a single-core processor, the CPU rapidly switches between multiple tasks using small time intervals, which gives the illusion of performing several tasks simultaneously.

The image illustrates concurrent processing on a CPU core, showing a sequence of tasks (Task 1, Task 2, Task 3, Task 1, Task 3, Task 1, Task 2, Task 4) being executed in an interleaved manner.

Understanding Concurrency

Concurrency refers to the ability to have multiple tasks or processes in progress at the same time. This is primarily achieved through multitasking, where tasks are interleaved on a processor. Note that concurrency is about managing multiple tasks, not necessarily executing them simultaneously.

Key Point

Concurrency enables more responsive applications by efficiently managing several tasks at once, even if they do not run in parallel.

The image explains "Concurrent Processing," defining concurrency as the notion of multiple things happening simultaneously, with the potential for multiple processes to be in progress at the same time.

Concurrency in Multicore CPUs

In multicore systems, the benefits of concurrency are amplified. For example, in a system with two cores and two tasks, each core can manage both tasks by switching between them, thereby maximizing the overall processing power. This approach, which leverages multiple cores to handle tasks concurrently, is known as multiprocessing.

The image illustrates concurrent processing with two CPU cores, each alternating between Task 1 and Task 2.

Concurrency vs. Parallelism

It's important to distinguish between concurrency and parallelism:

  • Concurrency involves managing multiple tasks at once, often through rapid task switching. An example is a text editor that lets you type and save a file simultaneously.
  • Parallelism uses multiple processing units to execute tasks simultaneously. A distributed data processing system that divides tasks across multiple clusters is a typical example of parallel processing.

The image provides examples of concurrent and parallel processing, with concurrent processing related to user-interactive programs and parallel processing related to distributed data processing.

Remember

While concurrency improves the efficiency and responsiveness of applications, parallelism takes advantage of hardware by executing tasks simultaneously across multiple cores or processors.

That concludes our discussion on sequential versus concurrent processing. Thank you for reading and happy coding!

Watch Video

Watch video content

Previous
Course Introduction