5.2.26 · D1 · HinglishC++ Programming

Foundationsstd - atomic — lock-free operations

2,651 words12 min read↑ Read in English

5.2.26 · D1 · Coding › C++ Programming › std - atomic — lock-free operations

Is page par assume kiya gaya hai ki aap kuch nahi jaante. Parent note (topic) padhne se pehle, aapko kuch ideas ki zaroorat hai. Hum inhe ek-ek karke build karte hain, har ek agले ka raasta kholega.


1. Memory, ek value, aur ek variable

School ke lockers ki ek row imagine karo. Har locker par ek number paint hai (address) aur andar ek note hai (value).

Figure — std - atomic — lock-free operations

Topic ko ye kyun chahiye: atomics jo kuch bhi karte hain woh sab ek box ke baare mein hai jisme do log haath daalte hain. Agar aap box imagine nahi kar sakte, toh "shared variable" sirf words hain.


2. Thread — apne haathon wala ek worker

Parent note ki saari problem is liye exist karti hai kyunki ek thread apne chhote steps ke beech mein pause ho sakta hai. Threads kaise create hote hain, ye jaanne ke liye std::thread and std::async dekhein.


3. Code ki ek line bahut saari machine steps hoti hain

Figure — std - atomic — lock-free operations

Topic ko ye kyun chahiye: "lost update" bug inhi teen steps ke beech ki gap mein rehta hai. Agar aap sochte hain counter++ ek step hai, toh bug samajhna naamumkin hai.


4. Read-Modify-Write (RMW) aur lost update

Figure — std - atomic — lock-free operations

Figure follow karo: red timeline dikhata hai Thread B 5 load kar raha hai jabki Thread A mid-flight hai. Yahi interleaving dushman hai. Ek atomic RMW steps 1–2–3 ko ek indivisible blink banata hai taaki koi beech mein na ghus sake.


5. Atomic — indivisible

Topic ko ye kyun chahiye: std::atomic<T> bilkul wahi tool hai jo 3-step RMW ko ek uncuttable step mein badalta hai.


6. Mutex — woh padlock jisse hum bachna chahte hain


7. Lock-free — aur uske cousins

Figure — std - atomic — lock-free operations

Topic ko ye kyun chahiye: "lock-free operations" literally title hai. Yahi payoff hai — padlock ki waiting ke bina atomicity — lekin aapko pata hona chahiye ki ye "koi thread kabhi wait nahi karta" ke barabar nahi hai (woh wait-free hai).


8. Cache aur coherency (MESI) — ek core akele kyun nahi jeet sakta

Figure — std - atomic — lock-free operations

Cache Coherency MESI aur False Sharing dekhein. Topic ko ye kyun chahiye: ye exclusive-ownership grab (invalidations se guard ki gayi E/M state) woh mechanism hai jisse hardware software lock ke bina atomicity provide karta hai.


9. Compare-And-Swap (CAS) — gate


10. Memory order — doosra promise

Topic ko ye kyun chahiye: threads mein correctness ke liye dono atomicity (§5) aur ordering (yeh) chahiye. Ek doosre ke bina bhi bugs aa sakte hain.


Foundations topic ko kaise feed karte hain

Map ko neeche se upar padho: upar ke ideas raw building blocks hain, aur har arrow ka matlab hai "samajhne ke liye zaroori hai." Ek memory cell aur ek thread apne private register ke saath aapko teen-step read-modify-write dete hain, jis ki interleaving lost-update bug karti hai. Woh bug atomic (indivisible) operation ka idea force karta hai. Atomicity, mutex padlock se bachne ki chahat ke saath, aur cache/MESI ownership se possible hoke, lock-free operation aur compare-and-swap gate deta hai. Ant mein atomicity memory order ke saath cross-thread visibility ke liye pair hoti hai. Ye saare arrows std::atomic topic node par converge karte hain — woh parent note jise aap padhne ki taiyaari kar rahe hain.

Memory cell

Thread

Register

Read Modify Write

Lost update bug

Atomic indivisible

Mutex padlock

Lock free

Cache and MESI

Compare And Swap

Memory order

std atomic topic


Equipment checklist

Khud ko test karo — sirf jawab dene ke baad reveal karo.

Memory cell mein kya store hota hai, aur use kya label karta hai?
Andar ek value (ek number); ek address use label karta hai.
Do threads simple code par bhi kyun collide kar sakte hain?
OS ek thread ko ek line ke tiny machine steps ke beech pause kar sakta hai.
counter++ ke andar chhupe teen steps kaun se hain?
register mein load karo, 1 add karo, memory mein wapas store karo.
Register kya hai aur ise kaun dekh sakta hai?
CPU core ke andar ek chhota fast slot; us core ke liye private jab tak wapas store nahi hota.
Read-modify-write define karo aur ek example do.
Ek op jo padhta hai, jo padha uske basis par badalta hai, phir wapas likhta hai — jaise x++.
"Atomic" intermediate states ke baare mein kya guarantee karta hai?
Koi bhi visible nahi; op ya toh pura ho chuka ya shuru nahi hua dikhai deta hai.
Mutex kaun sa cost add karta hai jise atomics avoid karne ki koshish karte hain?
Dusre thread ke lock rakhte waqt line mein wait karna.
Lock-free kaun si progress guarantee karta hai, aur kya nahi karta?
Koi na koi thread hamesha progress karta hai (system-wide); yeh kisi individual thread ko starve hone se nahi rokta.
Obstruction-free, lock-free, wait-free ko weakest se strongest rank karo.
obstruction-free < lock-free < wait-free (wait-free har thread ke apne steps bound karta hai).
Char MESI states ke naam batao.
Modified, Exclusive, Shared, Invalid.
Kaun si MESI state(s) core ko likhne deti hain, aur wahan pahunchne ke liye kya broadcast karta hai?
Modified ya Exclusive; woh ek invalidate broadcast karta hai taaki doosri copies Invalid ban jaayein.
Jab box ab expected nahi rakhta toh CAS kya karta hai?
Woh nahi likhta; current value ko expected mein load karta hai aur false return karta hai.
C++ ke chhe memory orders kaun se hain, aur default kaun sa hai?
relaxed, consume, acquire, release, acq_rel, seq_cst — default seq_cst hai.
Memory order kya control karta hai jo atomicity akele nahi karta?
Threads ke beech aas-paas ke non-atomic writes ki visibility aur ordering.