DEV Community

Ahmed_Greynoon
Ahmed_Greynoon

Posted on

Single and Multi-Threaded Programming with Concurrency and Parallelism - Explain The Differences and Concepts

In the world of programming, you definitely will face the concepts of single and multi-threaded programming, these concepts are closely tied to how a programming language handles concurrent execution and parallelism. In this article, we will explore the concepts and differences between them. But first, we need to understand concurrency and parallelism.

Also... why should we know about this? πŸ€” why would we bother ourselves to learn this? πŸ€”
trust me, I'm cooking something πŸ‘¨πŸ»β€πŸ³πŸ”₯ and everything is connected so be patient and read. πŸ€—

Concurrency Vs Parallelism

Image description

  • Concurrency: refers to the ability of a system to handle multiple tasks at once. However, this doesn't mean that the tasks are literally being executed simultaneously (at the exact same moment). Instead, concurrency allows multiple tasks to make progress during overlapping time periods.

  • Parallelism: occurs when two or more tasks run at the same time (simultaneously). They start, run, and complete in parallel, for example in your PC when your processor has more than one core, it is capable of running multiple threads at the same time. Each processor core can be working on a different task. This is a form of parallelism.

In a single-core processor, for example, it can only execute one instruction at a time. However, the operating system can rapidly switch between tasks, giving each task a small slice of time to execute before moving on to the next task. So, even though the processor is only executing one task at any given moment, it switches between tasks so quickly that it appears as though they are happening concurrently.

In multi-core processors also, for example, true parallelism can occur where different cores handle different tasks simultaneously. However, concurrency still applies because even within a single task, the system may break it down into smaller sub-tasks that are managed concurrently.

Image description

Understanding Single and Multi-Threaded Programming

Image description

Single-Threaded Programming

In single-threaded programming, a program operates on a single thread of execution. This means that the program executes one task at a time in a sequential manner, which means that a program follows the order of operations as defined in the code, executing one operation at a time. But if it encounters a time-consuming task (like for example waiting for a file to load or fetching data from a remote server), it must finish this task before moving on to the next.

This is inherently non-concurrent and non-parallel cause there's only one sequence of operations and in a single-threaded environment, there is no true concurrency because if tasks appear to run simultaneously they are actually being interleaved or managed through time-slicing, in which the opposite of what we said previously "The Concurrency refers to the ability of a system to handle multiple tasks at once", which creates the illusion of concurrency.

But for Parallelism, a Single-threaded environment cannot achieve true parallelism because there’s only one thread doing all the work.

Multi-Threaded Programming

In multi-threaded programming, a program runs multiple threads concurrently. These threads can perform different tasks simultaneously or handle multiple parts of a single task.

A multi-threaded program can split tasks into multiple threads, which can be run either concurrently or in parallel depending on the system's capabilities and the nature of the tasks.

In a multi-threaded environment, concurrency is achieved by allowing multiple threads to make progress on different tasks or parts of the same task. Concurrency doesn't necessarily mean that these tasks are being executed at the same time; it just means that they are being managed in such a way that it seems like they are happening simultaneously.

Parallelism occurs in a multi-threaded environment when multiple threads actually run at the same time on different cores of a multi-core processor. This is true parallel execution, where tasks are processed simultaneously, leading to potentially significant performance improvements.

Image description

Conclusion

Now, we understand the difference between concurrency and parallelism, Concurrency is managing multiple tasks that execute during overlapping periods which is often achieved on single-core processors, while parallelism is about executing multiple tasks at the same time which is possible on multi-core processors.

In single-threaded programming, tasks run sequentially without true concurrency or parallelism, but multi-threaded programming can run concurrently or in parallel improving performance by handling multiple tasks more efficiently.

Next article, we will learn more about these concepts and theories, and how to implement them using Dart programming language, and now I bet you know why 😁😁

Thank you for your time πŸ₯°πŸ₯°

I hope this article has made things clear to you.

Top comments (0)