Introduction of Multi-threading
Multi-threading एक ऐसी Programming Technique है, जिसमें एक ही Program के अंदर कई छोटे – छोटे Tasks (Threads) एक साथ Parallel या Concurrent रूप में चलते हैं, जिससे Application की Speed, Performance और Overall Responsiveness बेहतर हो जाती है।
Table of Contents
ToggleKey Points
- एक Program एक ही समय में कई operations perform कर सकता है।
- Multi-threading threads के through काम करता है, जो lightweight होते हैं।
- CPU का idle time कम होता है और performance बढ़ती है।
- सभी threads same memory space share करते हैं, इसलिए data access fast होता है।
- Processes heavy होते हैं, लेकिन threads lightweight, इसलिए memory usage कम होता है।
What is a Thread? (Thread क्या होता है?)
Thread किसी Program का सबसे छोटा Execution Unit होता है, जो किसी specific task को independently run करता है। इसे Lightweight Process भी कहा जाता है, क्योंकि यह Process की तरह heavy resources उपयोग नहीं करता।
Key Points
- Thread किसी Program का सबसे छोटा Runnable Unit होता है, जो एक specific task को execute करता है।
- यह कम Memory use करता है और Process की तुलना में तेज चलता है।
- एक ही Process के सभी Threads same memory space (variables, data, files) share करते हैं।
- Threads अपने-अपने tasks को independently execute कर सकते हैं।
- Thread के कारण Program multiple tasks parallel में कर सकता है —
जैसे UI Update, Downloading, Calculations। - Thread Switching, process switching से काफी तेज होता है, इसलिए Application smooth रहती है।
- हर Program में Starting में एक Main Thread बनता है, जो आगे sub-threads create करता है।
Why Multi-threading is Important?
(Multi-threading क्यों जरूरी है?)
1. To Increase Application Speed
- Multi-threading एक ही समय पर कई tasks को parallel में execute करती है, जिससे program जल्दी complete होता है।
- Example : Chrome में downloading, tab rendering और scrolling सब fast चलता है।
2. To keep the UI (User Interface) responsiv
- अगर पूरा program एक ही thread पर चले तो UI freeze/hang हो सकता है। Multi-threading में UI अलग thread पर run होता है, इससे app smooth रहती है।
- Example : Flipkart में images load होते रहते हैं, लेकिन scrolling रुकती नहीं।
3. To Increase CPU Utilization
- Modern CPUs (Dual-core, Quad-core, 8-core) multi-threading के लिए बनाई गई हैं।
- Single thread चलता है, तो बाकी CPU cores idle रहते हैं। Multi-threading सभी cores को busy और efficient बनाती है।
4. To Run Background Tasks Efficiently
- Mobile/PC apps को कई background tasks की जरूरत होती है :
- Media Uploading
- Notifications Sync
- Auto-backup
- Downloading
- File Scanning
इन सबको एक साथ smoothly चलाने के लिए multi-threading जरूरी है।
5. For Real-Time Performance
- Games, video-editing tools, GPS tracking apps real-time data process करती हैं, Single thread इसे handle नहीं कर सकता।
- Example : PUBG में sound, animation, networking, joystick actions—all अलग threads पर चलते हैं।
6. Essential for Large-Scale Systems
- Banking Systems, IRCTC, UPI Servers, Cloud Platforms रोज millions of requests handle करते हैं।
- यह concurrency multi-threading से possible होती है।
7. For a Better Multitasking Experience
- User एक साथ multiple actions कर सकता है —
- scrolling
- video देखना
- notifications receive करना
- background downloading
यह सब multi-threading की वजह से seamless लगता है।
8. For Time-Saving and Efficient Processing
- Heavy tasks जैसे — Image Processing, AI Computations, Database Queries parallel में चलकर time बचाते हैं और performance improve करते हैं।
Purpose of Multi-threading
(Multi-threading के मुख्य उद्देश्य)
1. Speeding Up Program Execution
- Multi-threading बड़े काम को कई छोटे tasks में बाँटकर parallel चलाती है, जिससे program जल्दी execute होता है।
2. Making Applications Hang-Free and Responsive
- Single-threaded applications heavy task आने पर freeze हो जाती हैं।
- Multi-threading UI thread को अलग रखती है, जिससे user को smooth experience मिलता है।
3. Better CPU Utilization
- Multi-core CPUs आज हर device में हैं। Multi-threading इन cores पर work distribute करके processing power maximize करती है।
4. Handling Background Tasks Efficiently
- Downloading, notifications, backups, auto-sync जैसे tasks background threads पर run होते हैं और main thread free रहता है।
5. Making Heavy Tasks Faster by Dividing
- Multi-threading long operations जैसे video rendering, scientific calculations, data filtering को छोटे – छोटे parallel jobs में convert करके speed बढ़ाती है।
6. Supporting Real-Time Operations
- Games, GPS navigation, OTT streaming apps real-time data पर depend करती हैं। Multi-threading बिना delay updates देने में मदद करती है।
7. Providing Concurrency
- Server systems (जैसे UPI, IRCTC, Banking) को एक समय में हजारों requests handle करनी पड़ती हैं। Multi-threading concurrency देकर यह काम आसान कर देती है।
8. Making Resource Sharing Efficient
- Threads एक ही memory और resources को share करते हैं, जिससे memory usage कम होता है और execution तेज होता है।
How Multi-threading Works
(Multi-threading कैसे काम करता है?)
Step-by-Step Working
1. Main Thread
- हर C++ program start होने पर main thread create होता है।
- यह main() function के execution के लिए responsible होता है।
int main() {
std::cout << “Main thread running\n”;
}
2. Creating Additional Threads
- Main thread या कोई existing thread new threads create कर सकता है।
#include <iostream>
#include <thread>
void task1() {
std::cout << “Task 1 running\n”;
}
int main() {
std::thread t1(task1); // new thread created
t1.join(); // wait for t1 to finish
return 0;
}
- t1.join() thread को complete होने तक main thread को wait कराता है।
- t1.detach() thread को independent run करने देता है, main thread wait नहीं करती।
3. Executing Threads
- CPU scheduler threads को time slices देता है।
- Multi-core CPU पर multiple threads true parallel में execute हो सकते हैं।
void task2() {
for(int i=0; i<5; i++)
std::cout << “Task 2 running ” << i << “\n”;
}
int main() {
std::thread t1(task1);
std::thread t2(task2);
t1.join();
t2.join();
}
4. Synchronization And Shared Resources
- Multiple threads shared data access कर सकते हैं , race condition हो सकती है।
- C++ में synchronization के लिए mutex use होता है |
#include <mutex>
std::mutex mtx;
void safeTask() {
mtx.lock();
// critical section
std::cout << “Thread safe execution\n”;
mtx.unlock();
}
- इससे data safe रहता है और concurrency issues avoid होते हैं।
Types of Threads (थ्रेड के प्रकार)
1. User Threads (यूज़र थ्रेड)
User threads वो threads होते हैं, जिन्हें application level पर developers directly create और control करते हैं।
Key Points
- Thread scheduling OS नहीं, बल्कि JVM (Java Virtual Machine) या Programming environment manage करता है।
- Application-specific tasks के लिए use होते हैं।
- Heavy computation और background tasks manage करने में helpful।
2. Kernel Threads (कर्नेल थ्रेड)
Kernel threads वो threads होते हैं, जिन्हें Operating System (OS) directly manage करता है।
Key Points
- Thread creation और scheduling OS handle करता है।
- Hardware level पर efficiently run होते हैं।
- Multiprocessing और multi-core CPU systems में performance enhance करते हैं।
3. Daemon Threads (डेमॉन थ्रेड)
Daemon threads ऐसे background threads होते हैं, जो system services या background tasks को handle करते हैं।
Key Points
- Application exit होने पर automatically terminate हो जाते हैं।
- User interact नहीं करते।
- Lightweight और continuous running tasks के लिए use होते हैं।
Advantages (लाभ)
- Increasing Program Speed (Faster Execution) : Multiple tasks parallel execute होने से program जल्दी complete होता है।
- Making the Application Responsive (Smooth UI) : Heavy background tasks separate thread पर चलते हैं, जिससे user interface hang या freeze नहीं होता।
- Maximizing CPU Utilization (Efficient CPU Usage) : Multi-core CPU में threads parallel run करके CPU का पूरा उपयोग करते हैं।
- Efficient Resource Management : Threads Process के memory और resources share कर सकते हैं, जिससे resource waste कम होता है।
- Better Performance in I/O Operations : Input/Output operations जैसे file read/write या network requests background में चलते हैं, main thread free रहता है।
- Simplifies Program Structure for Concurrent Tasks : अलग-अलग tasks को अलग-अलग threads में divide करके program design आसान हो जाता है।
- Background Services and Daemon Tasks Support : Background operations जैसे logging, monitoring, garbage collection thread-based implement होते हैं।
Disadvantages (हानियाँ)
- Increased Code Complexity : Multiple threads होने से program का structure complex हो जाता है।
- Risk of Deadlock & Race Condition : जब दो या ज्यादा threads एक resource के लिए एक साथ wait करते हैं, तो program hang हो सकता है।
- Debugging Difficult : Multi-threaded programs debug करना normal programs की तुलना में बहुत challenging होता है।
- Performance Overhead (Thread Management का Extra Load) : Thread creation, context switching और synchronization में CPU time लग सकता है।
- Silent Errors/Hidden Bugs : Incorrect synchronization या empty catch blocks से subtle bugs पैदा हो सकते हैं।
- Overuse leads to Misuse : Normal tasks के लिए unnecessary threads use करने से program inefficient और complicated बन जाता है।






