How Does Concurrency and Multithreading Work?

153.8K views
•
March 6, 2022
by
CodeHelp - by Babbar
YouTube video player
How Does Concurrency and Multithreading Work?

TL;DR

Concurrency divides a process into independently executable threads, allowing separate tasks to progress without blocking the entire application. Threads share their process's address space, text, data, and heap, but each has its own stack, program counter, and thread control block. Multithreading improves responsiveness, resource sharing, switching efficiency, and multicore CPU utilization.

Transcript

Hello ji , how are you all ? this is Lakshay We have reached lecture no-15 , today we will start a new module in OS , Concurrency In this we will study about threads , the difference between threads and process In lecture no-3 or 4 we were discussing about multitasking VS multi threading differences , revise it Before watching this see that lect... Read More

Key Insights

  • Concurrency is the operating system's ability to execute multiple instruction paths by dividing a process into threads that can progress independently. When suitable threads run on separate CPU cores, the process gains parallel execution and can complete independent work more quickly.
  • A thread is a single sequence of execution within a process and is described as a lightweight process. A useful threading design separates a larger task into independent paths, such as text editing, spell checking, and formatting within a word-processing application.
  • Threads share the address space of their parent process, while separate processes have individual address spaces. Shared memory gives threads direct access to common process resources and avoids the communication gap and larger overhead associated with communication between isolated processes.
  • Each thread has its own program counter and thread control block. A thread scheduling algorithm determines CPU access, and runtime priority can influence which thread receives more processor time before the operating system fetches and executes its next instruction.
  • Thread context switching is faster than process context switching because threads from the same process retain the same address space. The operating system changes the program counter and registers, while avoiding a memory-space switch and the need to reset the CPU cache.
  • A single CPU cannot execute two threads in parallel because only one thread can use that CPU at a time. Multiple cores allow independent threads to run simultaneously, such as assigning T1 to one CPU and T2 to another.
  • Thread memory organization includes shared text, data, and heap regions, plus a separate stack for every thread. Therefore, the number of stacks corresponds to the number of threads, while the process continues to use a single shared heap.
  • Multithreading improves responsiveness, resource sharing, economy, and multicore utilization. If one thread blocks while performing input, output, internet fetching, or another waiting operation, other threads can continue their work instead of blocking every task in the process.

Explore YouTube Video Summarizer or Get YouTube Transcript Extractor

Questions & Answers

Q: What is concurrency in an operating system?

Concurrency is the operating system's ability to execute multiple instruction paths by dividing a process into independent threads. Each thread represents a separate execution path within the same process. When more than one CPU core is available, different threads can run on different cores simultaneously, providing parallel execution for tasks that can operate independently.

Q: What is a thread, and how is it related to a process?

A thread is a single sequence of execution within a process and is also described as a lightweight process. It represents an independent path through part of the process's work. For example, text editing, spell checking, and formatting can become separate threads while remaining parts of the same word-processing process.

Q: How do threads differ from separate processes in memory usage?

Separate processes receive individual address spaces, while threads created within one process use the same address space. Threads share the process's text, data, heap, and other available resources. However, every thread has its own stack, program counter, and thread control block so that its execution state can be scheduled and maintained independently.

Q: How does a thread get access to the CPU?

A thread receives CPU access through a thread scheduling algorithm. Each thread has a thread control block and its own program counter, which identifies the instruction associated with that execution path. The operating system schedules threads at runtime, can consider their priorities, and fetches the instruction corresponding to the selected thread's program counter.

Q: Why is thread context switching faster than process context switching?

Thread context switching is faster because switching between threads of the same process does not require changing the address space. The operating system saves the current thread's state and switches program counters and registers. Because the process memory remains the same, there is less switching overhead and no need to reset the CPU cache.

Q: Does multithreading provide parallelism on a single CPU?

Multithreading does not provide parallel execution when only one CPU is available, because one thread must use the processor while the other waits. The threads are scheduled sequentially in that situation. The stated performance advantage appears with multiple CPUs or cores, where independent threads can be assigned to different processors and execute simultaneously.

Q: What are the main benefits of multithreading?

The main benefits are responsiveness, efficient resource sharing, economy, and improved multicore CPU utilization. One blocked thread does not have to stop every other task, and threads can directly use shared process resources. Thread creation and switching also require less overhead than separate processes, while multiple cores can execute independent paths simultaneously.

Q: Why does the demonstrated C++ multithreading program produce an error?

The demonstration creates two worker threads, T1 and T2, for two independent printing tasks, while the original main thread remains active. This produces three threads in total. The worker threads begin independently, but the main thread has no waiting step in the shown code and attempts to exit while the worker threads are still executing, causing the error.

Summary & Key Takeaways

  • Concurrency allows multiple independent instruction paths within a process to make progress through threads. A thread is a single sequence of execution, sometimes described as a lightweight process. When separate tasks are assigned to different CPU cores, they can execute in parallel and reduce the time required for suitable workloads.

  • Threads belonging to one process share the same address space, text, data, and heap, while each thread has a separate stack, program counter, and thread control block. This organization makes resource sharing and context switching less expensive because switching between threads of the same process does not replace the address space.

  • The C++ demonstration divides two independent printing tasks into separate threads named T1 and T2. The process also retains its main thread, producing three threads in total. Because the created threads begin executing independently, the main thread can attempt to exit before they finish, which causes the demonstrated program error.


Read in Other Languages (beta)

Share This Summary 📚

Explore More Summaries from CodeHelp - by Babbar 📚