5.2.26 · D3 · HinglishC++ Programming

Worked examplesstd - atomic — lock-free operations

3,679 words17 min read↑ Read in English

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


Scenario matrix

Kisi bhi code se pehle, aao list karte hain har tarah ki situation jo atomics aap par throw kar sakti hai. Ise ek checklist samjho — agar hum ek example per row karte hain, toh aap kabhi koi unseen case nahi dekhenge.

Cell Scenario class Woh core question jo yeh test karta hai
A Simple RMW, koi cross-variable dependence nahi Kya fetch_add lost updates fix karta hai? Kya relaxed kaafi hai?
B RMW jo built-in op nahi hai (custom function) Kya hum phir bhi CAS retry loop se atomic ho sakte hain?
C High-contention limiting case Jaise retries badhti hain kya hota hai? Kya progress phir bhi milti hai?
D Cross-variable visibility (publish/subscribe) Hume acquire/release kyun chahiye, relaxed nahi?
E a = a + 1 degenerate trap Ek "obvious" atomic line ek atomic op kyun nahi hai?
F Zero / boundary inputs (empty pop, single winner) Kya hoga agar structure empty ho, ya do threads ek saath try karein?
G Real-world word problem Ticket-booking seat allocation — ek seat, bahut saare buyers
H Exam-style twist weak spurious failure aur "ABA" surprise

Ab hum sare aath cells fill karte hain.


Cell A — simple atomic counter


Cell B — custom RMW via a CAS loop

Neeche wahi loop ek flow ke roop mein draw hai: read → compute → CAS commits, single-thread "no contention → immediate win" path amber mein highlighted hai. White arrows left-to-right follow karo, phir success box mein neeche jaao.

Figure — std - atomic — lock-free operations

Cell C — high contention limiting behaviour


Cell D — cross-variable visibility (acquire/release)

Neeche ka figure ise do threads ke beech ek one-way gate ki tarah draw karta hai: amber synchronizes-with arrow ek hi channel hai, aur producer ne apne release ke upar jo bhi likha woh sab iske through force hota hai consumer ke acquire se pehle jo execution proceed karne deta hai. Producer column top-down padhon, amber arrow cross karo, phir consumer column padhon.

Figure — std - atomic — lock-free operations

Cell E — a = a + 1 degenerate trap


Cell F — zero / boundary inputs (empty stack, single winner)


Cell G — real-world word problem: ek seat, bahut saare buyers


Cell H — exam twist: spurious failure aur ABA surprise


Recall Quick self-test: cell ka naam batao

fetch_add(1, relaxed) counter sahi hai kyunki... ::: har op ek indivisible RMW hai; relaxed atomicity rakhta hai, sirf cross-variable ordering drop hoti hai (Cell A). Worst-case CAS failures colliding threads ke liye ::: , quadratic — lock-free wait-free nahi hai (Cell C). a = a + 1 atomic par abhi bhi buggy kyun hai? ::: yeh load-then-store hai (do ops ek gap ke saath), ek RMW nahi; fetch_add/++a use karo (Cell E). Lock-free push mein, successful CAS ko release-store kyun hona chahiye? ::: taaki new node ke fields (v, next) head pointer visible hone se pehle publish hon; warna popper uninitialized data padh le (Cell F). Ek seat, chaar buyers, kitne succeed karte hain? ::: exactly ek; CAS ek single winner deta hai (Cell G). Kya spurious weak failure final value change karta hai? ::: nahi — sirf extra loop iterations ki cost lagti hai; correctness untouched rehti hai (Cell H).


Parent: Hinglish version · Related: Compare-And-Swap · Memory Model (C++) · Cache Coherency MESI · std::thread and std::async · False Sharing · Mutex and Lock

Flashcards

threads ek loop par collide karein toh worst-case failed CAS attempts kitne?
Arithmetic series , jo dikhata hai lock-free wait-free nahi hai.
Ticket-booking (ek seat, chaar buyers) example mein compare_exchange_strong kyun use karte hain, weak nahi?
Yeh ek single non-looped check hai, toh spurious failures tolerate nahi kar sakte; strong kabhi spuriously fail nahi karta.
ABA problem kya hai?
CAS values compare karta hai history nahi; agar ek value A se B aur wapas A jaaye, CAS galat tarike se succeed karta hai. Fix: version tags ya hazard pointers.
Lock-free stack push mein successful CAS ko memory_order_release kyun use karna chahiye?
Taaki new node ke initialized fields head pointer visible hone se pehle publish hon, taaki acquiring popper kabhi uninitialized data na padhe.