Multithreading in C++

What is Multithreading?

  • Multithreading means different tasks or processes are executed at the same time on different cores or processors.
  • It is a specialized form of multitasking. Multitasking allows to run two or more programs concurrently.
  • Multithreading creates a process that consists of multiple threads of execution(thread).
  • It is used when the parallel execution of some tasks leads to more efficient use of resources of the system.
  • Multithreading is a natural choice for handling event-driven code which is mostly common in today's highly distributed, networked, GUI-based environments.
  • It can be applied to one process to enable parallel execution on a multiprocessing system.

What is Thread?

  • Thread is the smallest sequence of programming instructions that can be managed by a scheduler.
  • It is a dispatchable unit of executable code.
  • These threads can run parallel and increases efficiency of programs.
  • Each part of a program is called a Thread, and each thread defines a separate part of execution.
  • If thread is in need of more than one resource then it requires more than one lock and it is possible to use more locks. If any lock is not available, the thread will wait or block on the lock.

Threads v/s Processes

  • Threads and processes are related to each other but they are fundamentally different.
  • Process is an instance of a running program. System resources such as CPU time, memory etc. are allocated to each process and executed in a separate address space. Each process is an independent entity and communicates with each other through inter-process communication.
  • Process provides two key abstractions such as Logical control flow(CPU) and Private virtual address space(Main memory) for each process.

  • application processing

  • Process can have multiple threads and thread uses the same address space of a process.
  • The only difference between threads and processes is that multiple threads share parts of their state(eg. ready, running, waiting or stopped) as well as memory and other resources.
  • In multithreading, threads require less overhead to manage than processes and intraprocess thread communication is less expensive than interprocess communication.
  • In multiple process, each process can execute on a different machine (Distribute Program) for example, file servers (NFS), FTP, remote log-in clients and servers (Telnet), web browsers and servers etc.
  • structure of memory block
In the above figure, it shows the structure of memory block where single thread and multithread are performing. It shows that a thread has state like a process does, but its state are the values which held in its register and the data on its stack.

Following are the differences between Multiprocessing and Multithreading.

A process is the unit of resource allocation and protection.A thread is the unit of computation that runs in the context of a process.
It manages certain resources for example, virtual memory, I/O handlers and signal handlers.It manages certain resources for example, stack, registers, signal masks, priorities and thread-specific data.
It is protected from other processes via an MMU.Threads can interfere with each other.
Interprocess communication between processes can be complicated and inefficient.Intraprocess communication between threads are more efficient than Interprocess communication between processes.

How to create a Thread?

  • The header file thread.h provides functionality for creating multithreaded C++ programs.
  • First include thread header file and then create an object of a thread class.
thread t;

Advantages of Multithreading

  • Multithreading performs faster on a multi-CPU system.
  • It improves performance and concurrency.
  • It simplifies the coding of remote procedure calls and conversions.
  • It accesses multiple applications simultaneously.
  • It reduces number of required servers.
  • It enables a program to make the best use of available CPU cycles and allows to write very efficient programs.

Disadvantages of Multithreading

  • All threads in a process share the same memory space. If one thread crashes the process, it affects the entire process, because it shares the same memory space.
  • Multithreading has difficulties in finding and resolving bugs.
  • Synchronization issues make debugging multithreaded programs more difficult than single-threaded programs.