5.2.24 · D1 · HinglishC++ Programming

FoundationsConcurrency — std - thread, std - mutex, std - lock_guard, std - unique_lock

1,986 words9 min read↑ Read in English

5.2.24 · D1 · Coding › C++ Programming › Concurrency — std - thread, std - mutex, std - lock_guard, s

Parent note padhne se pehle, tumhe har word aur symbol ka matlab dekhna hoga. Hum inhe ek-ek karke, neeche se upar tak, build karte hain. Neeche jo bhi hai woh koi aisa term use nahi karta jo pehle draw nahi hua.


0. Ek program actually kya kar raha hota hai? (thread ka "picture")

Picture: ek to-do list jisme ek ungli current line par point kar rahi hai.

Figure — Concurrency — std - thread, std - mutex, std - lock_guard, std - unique_lock

Figure dekho: left column ek thread hai (ek ungli), right mein ek doosri ungli independently move kar rahi hai. Dono memory ke same boxes ko read aur write kar sakti hain — wahin se musibat shuru hoti hai.


1. Core (CPU) — jahan ek ungli actually run hoti hai

Yeh topic kyun mention karta hai: parent kehta hai concurrency exist karti hai kyunki cores idle baithte hain. Ek ungli ek core use karti hai; baaki cores kuch nahi karte. Ek doosri thread ek doosre core ko kaam par lagati hai → zyada steps per second hoti hain (throughput).


2. Shared memory — woh boxes jinhe dono ungliyan touch karti hain

Shared memory ka matlab hai: wahi box dono ungliyon ko visible hai. Yeh powerful (cooperation) bhi hai aur dangerous (collision) bhi, jo agli section ko motivate karta hai.


3. Read–modify–write ka picture (kyun ++counter actually TEEN steps hain)

Parent claim karta hai ++counter ek action nahi balki teen hai. Yeh raha woh picture jo ise prove karta hai.

Figure — Concurrency — std - thread, std - mutex, std - lock_guard, std - unique_lock

4. Data race — bimari, draw ki gayi

Recall "Kam se kam ek likhta hai" kyun?

Do ungliyan sirf ek hi box ko read kar rahi hain toh kabhi conflict nahi hota — reading se woh change nahi hota. Khatre wali baat tab hai jab ek finger read ya write kar rahi ho aur doosra write ho raha ho. ::: Kyunki ek write box ko beech mein change kar deta hai, isliye doosri ungli jo dekhti hai woh luck (timing) par depend karta hai.

Link: yeh exactly Undefined behavior in C++ hai — compiler assume karta hai yeh kabhi nahi hota, isliye observed result bakwaas ho sakta hai, sirf "kabhi-kabhi galat" nahi.


5. Serialize — ek word mein ilaaj

Ek mutex (agla) ka poora point yahi hai ki ek chosen region ko serialized banao.


6. Mutex — single key

Figure — Concurrency — std - thread, std - mutex, std - lock_guard, std - unique_lock

Figure mein key dono ungliyon ko ek queue mein force karti hai: finger A key hold karti hai aur apna pura read-modify-write karta hai; finger B darwaze par atki rehti hai jab tak A key return nahi kar deta. Race gone.


7. Scope, construction, destruction — woh machinery jis par RAII sawaar hai

lock_guard samajh mein aane se pehle, tumhe scope dekhna hoga.

Yeh guaranteed-cleanup rule RAII and resource management ki poori foundation hai — "constructor mein resource lo, destructor mein release karo".


8. lock_guard & unique_lock — RAII key ke around wrap kiya gaya


9. Angle-bracket aur :: notation jo tumhe baar baar milegi


Prerequisite map

Program counter = a moving finger

Thread = one finger

Core = one worker

Variable = a box in memory

Shared memory

Read modify write = 3 steps

Data race = lost step

Undefined behavior

Mutex = single key

Block = park and wait

Scope and destructor = guaranteed cleanup

RAII

lock_guard and unique_lock

Safe concurrency

Har row apne neeche waale ko feed karti hai. Jab tak tum lock_guard tak pahunchte ho, har pehla word already ek picture rakhta hai.


Equipment checklist

Khud ko test karo — tum parent note ke liye ready ho jab tum bina dekhhe har cheez ka jawab de sako.

Thread kya hai, ungli waale picture mein?
Instructions ki ek list par step karne waali ek chalti hui ungli; concurrency = ek saath do ya zyada ungliyan.
Ek core tumhe kya deta hai?
Doosron ke saath exactly ek ungli (thread) truly simultaneously run karne ki ability.
++counter ek operation kyun nahi balki teen operations hain?
Yeh hai box read karo, scratch pad par ek jodo, wapas likho — ek doosri ungli beech mein slip kar sakti hai.
Data race ko teen conditions mein define karo.
Same memory box, kam se kam ek writer, unhe order karne wala koi synchronization nahi — jo C++ mein undefined behavior hai.
Ek mutex physically kya model karta hai?
Ek single key jo sirf ek ungli hold kar sakti hai; lock() ise uthata hai (hold hone par block karta hai), unlock() return karta hai.
Ek thread ke liye "block" ka matlab kya hai?
OS thread ko kuch nahi karte hue park kar deta hai jab tak jis key ka woh wait kar raha hai woh free na ho jaaye.
Destructor kabhi skip kyun nahi ho sakta?
Scope ki closing brace cross karna hamesha andar bane objects ko destroy karta hai — jaldi return ya exception par bhi.
lock_guard ka one-line kaam kya hai?
Apne constructor mein mutex lock karo, destructor mein unlock karo, taaki key scope exit par hamesha return ho.
Tum unique_lock kab reach karte ho?
Jab tumhe extra buttons chahiye — jaldi unlock, deferred lock, move, ya ek condition variable.
Kisi naam se pehle std:: ka matlab kya hai?
Woh naam standard library ke std namespace (drawer) mein rehta hai; :: use reach karta hai.