C++ में Mutex और Locks
In this page:
#include <mutex>
std::mutex mutex_name;
mutex_name.lock();
// critical section
mutex_name.unlock();
std::lock_guard<std::mutex> guard(mutex_name); // unlocks automatically
Data Races और Mutex
एक data race तब होती है जब कई threads एक ही समय पर same shared memory location में लिखने की कोशिश करते हैं, corrupted data की ओर ले जाते हुए। आप shared data को एक 'std::mutex' से protect कर सकते हैं यह सुनिश्चित करने के लिए कि एक बार में सिर्फ एक thread इसे access कर सके।
उदाहरण: Data Races and Mutex
#include <iostream>
#include <mutex>
std::mutex m;
int counter = 0;
void increment() {
m.lock();
counter++; // protected from concurrent writes
m.unlock();
}
int main() {
increment();
std::cout << counter << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Lock Guard (std::lock_guard)
Manual lock() और unlock() इस्तेमाल करना dangerous हो सकता है क्योंकि अगर आपका code एक exception throw करे या early return करे, mutex हमेशा के लिए locked रह जाता है। एक mutex lock करने के लिए 'std::lock_guard' इस्तेमाल करें। यह scope से बाहर जाने पर automatically mutex unlock करता है।
उदाहरण: Lock Guard (std::lock_guard)
#include <iostream>
#include <mutex>
std::mutex m;
int counter = 0;
void increment() {
std::lock_guard<std::mutex> lock(m); // auto-unlocks, even on early return/exception
counter++;
}
int main() {
increment();
std::cout << counter << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Unique Lock (std::unique_lock)
'std::unique_lock' class lock_guard का एक ज़्यादा flexible version है। यह आपको किसी भी point पर manually mutex lock और unlock करने, deferred lock acquisitions, और time-delayed locks manage करने देती है।
उदाहरण: Unique Lock (std::unique_lock)
#include <iostream>
#include <mutex>
std::mutex m;
int main() {
std::unique_lock<std::mutex> lock(m); // can unlock/relock manually
lock.unlock();
lock.lock();
std::cout << "Manually managed lock" << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Deadlocks
एक deadlock तब होता है जब दो threads हमेशा के लिए block हो जाते हैं, हर एक दूसरे से hold किए एक mutex का wait करते हुए। Deadlocks से बचने के लिए, कई mutexes को safely और simultaneously lock करने के लिए C++17 का 'std::scoped_lock' इस्तेमाल करें।
उदाहरण: Deadlocks
#include <iostream>
#include <mutex>
std::mutex m1, m2;
int main() {
std::lock(m1, m2); // locks both without risking a deadlock, unlike locking separately
std::lock_guard<std::mutex> lock1(m1, std::adopt_lock);
std::lock_guard<std::mutex> lock2(m2, std::adopt_lock);
std::cout << "Both locked safely" << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Shared Memory के लिए Best Practices
अपने critical sections (आपके code के वे हिस्से जो locked हैं) जितना हो सके छोटा रखें। यह सुनिश्चित करता है कि threads locks के लिए कम time wait करने में खर्च करें, जो program throughput improve करता है।
उदाहरण: Best Practices for Shared Memory
#include <iostream>
#include <mutex>
std::mutex m;
int counter = 0;
void increment() {
{
std::lock_guard<std::mutex> lock(m); // critical section kept as small as possible
counter++;
}
std::cout << "Non-critical work outside the lock" << std::endl;
}
int main() {
increment();
std::cout << counter << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- manually locked एक mutex unlock करना भूल जाना।
- अलग-अलग threads में दो mutexes अलग-अलग order में lock करना, जो deadlock cause करता है।
- lock hold किए बिना shared data access करना, जो एक data race cause करता है।
Chapter Quiz — Complete all 17 topics to unlock
0/17 topics done
Complete these topics first:
- C++ vs C Differences
- C++ Interview Questions
- C++ Debugging Techniques
- C++ Input Validation
- C++ Namespaces
- C++ Header Files
- C++ Multi-file Programming
- C++ static_cast
- C++ dynamic_cast
- C++ const_cast
- C++ reinterpret_cast
- C++ Threads (std::thread)
- C++ Mutex & Locks
- C++ async & future
- C++ Mini Project — Calculator
- C++ Mini Project — Student Management
- C++ Interview Questions Advanced