“This is the 19th day of my participation in the First Challenge 2022. For details: First Challenge 2022”

Hello, everyone. I’m a fisherman from Go School.

Recently, a friend of mine asked me about concurrency and parallelism. As I explained his question to him, we started talking about other related concepts and terms, such as threads: multithreaded versus single-threaded, asynchronous versus synchronous. At this point, we’re all puzzled by questions like:

  • What is the relationship between concurrency and parallelism?

  • What is synchronous execution and what is asynchronous execution?

  • What is the importance of synchronous and asynchronous programming in concurrency and parallelism?

  • How do threads match these concepts?

01 Concurrency and parallelism

concurrent

Imagine a situation where you have to sing and eat at the same time. At some point, you either sing or you eat, because both things have to do with your mouth. So in order to do both tasks at the same time, you eat a meal, then sing a song, and then repeat until the meal is eaten or the song is sung. So, you’re doing it concurrently.

You can sing and dance at some point, but not at the same time.

Concurrency means that multiple tasks can be processed at the same time, but only one task can be executed at a time, and the execution between multiple tasks is not necessarily sequential. In concurrent applications, two tasks can be started, run, and completed alternately over a period of time. For example, task 2 can be started before task 1 completes.

In computer science, different cpus implement concurrency in different ways. In a single-core CPU environment (for example, your processor is single-core), concurrency is achieved by switching the context of the process. For a multi-core CPU, concurrency can be achieved in parallel.

Perform tasks in a single core environment. Context switches between tasks.

parallel

Let’s say you have two tasks: cooking and talking to a friend on the phone. You can do both at the same time. You can call your friends on your cell phone while cooking. So now you’re doing things in parallel.

Parallelism means the ability to perform multiple tasks at the same time. In computer science, parallel computing refers to the process of performing multiple computations simultaneously.

Two tasks are executed at the same time in the same period

What’s the relationship between parallelism and concurrency?

  • Concurrency and parallelism are concerned with how tasks or computations are performed in a computer architecture.
  • In a single-cpu environment, concurrency is achieved by context switching at the same time cycle. That is, only one task is executing at a given time.
  • In a multi-core environment, concurrency is achieved by executing multiple tasks concurrently.

02 Threads & Processes

Threads A thread is a single sequentially executed piece of code that can be executed independently. It is the smallest unit that an operating system can execute. A program can be either single-threaded or multithreaded.

process

A process is an instance of a program running. A program can have more than one process (start a program several times, each process has its own running environment, does not affect each other). When a process starts, it usually starts only one thread, such as a primary thread. But as the program executes, it can create more threads.

The distribution of processes and threads in a program

03 Synchronous and Asynchronous

synchronous

Imagine you have to write two letters, one to your mother and one to your best friend. You can’t write two letters at once unless both hands can write at the same time.

In the synchronous program model, tasks are executed one after another. Each task can be started only after the previous task is completed.

Imagine that you have two tasks: making sandwiches and washing clothes in a washing machine. You can put your clothes in the washing machine and make sandwiches without waiting for it to finish. These two tasks are executed asynchronously.

In the asynchronous programming model, when a task has been executed, you can switch to another task without waiting for it to complete.

Synchronization and asynchrony in single-threaded and multithreaded environments

Synchronous – Single thread:

The tasks are carried out one by one. Each task needs to wait for the completion of the previous task.

Synchronous – Multithreading:

Each task is executed in a different thread, but waits for the lead task to complete

Asynchronous – Single thread

Task execution does not need to wait for other tasks to complete. But only one task can be executed at a time.

Asynchronous – multithreading

Task execution does not need to wait for other tasks to complete. But multiple tasks can be executed at the same time.

What are the roles of synchronous and asynchronous programs in concurrency and parallelism?

  • The asynchronous program model helps us achieve concurrency
  • The asynchronous program model in multithreading is a way to realize concurrency.

4 conclusion:

Concurrency and parallelism refer to the way tasks are executed. Synchronous and asynchronous refer to the communication programming model. Single-threaded and multithreaded refer to the environment in which tasks are executed.

Reference connection:

  • Softwareengineering.stackexchange.com/questions/1…
  • Stackoverflow.com/questions/7…
  • Codewala.net/2015/07/29/…
  • youtu.be/cN_DpYBzKso
  • Medium.com/flawless-ap…

Original link: medium.com/swift-india…