KIS problem class mein: concurrent unsynchronized access se undefined behavior (torn reads/writes, lost updates, visibility bugs). C/C++ mein ek data race seedha undefined behavior hai; compiler kuch bhi kar sakta hai.
Hum chahte hain ki har memory location ke liye answer milein: "kya yeh do accesses ordered hain?" Hum data structure ko step by step derive karte hain.
Ek single counter track nahi kar sakta ek thread doosron ke baare mein kya jaanta hai. Isliye thread t ek vector clockVt rakhta hai: ek array jisme har thread ke liye ek entry hoti hai.
Vt[u]=the latest event index of thread u that t is guaranteed to see
Yeh step kyun?Vt encode karta hai "woh sab kuch jo thread t mein abhi tak happens-before hua hai." Agar thread A ki B ke baare mein current knowledge B ki write ko already cover karti hai, toh woh write happened-before hai — koi race nahi.
Rules (happens-before axioms se derive kiye gaye):
Local tick: apne sync event par, Vt[t]+=1.
Release (unlock L): Vt ko lock ke clock mein copy karo, VL←VL⊔Vt (componentwise max).
Acquire (lock L): lock ka knowledge absorb karo, Vt←Vt⊔VL.
Yeh exactly axiom 2 (release/acquire) ko mechanical banata hai: max histories ko merge karta hai.
Har application byte ke liye, TSan shadow cells store karta hai jo kuch pichle accesses describe karte hain: kaun sa thread, us waqt thread ki clock value, aur read/write flag.
Jab thread t location x ko access karta hai, thread u (clock k) ka pehle se record kiya gaya access dekho:
race iff (at least one is a write)∧(Vt[u]<k)
Recall Feynman: ek 12-saal ke bacche ko explain karo
Imagine karo do bacche ek hi Google Doc edit kar rahe hain. Agar woh baari-baari karte hain (ek khatam karta hai, phir doosre ko bolta hai "teri baari"), toh kabhi mess nahi hoti. Lekin agar dono bina ek doosre ko bataye ek hi line mein type karte hain, toh words scramble ho jaate hain — aur yeh sirf kabhi-kabhi scramble hota hai, toh tum dobaara padh ke pakad nahi sakte.
TSan ek hall monitor ki tarah hai jo har baar likhta hai jab ek baccha kuch likhta hai, aur yeh bhi note karta hai jab bhi koi baccha actually note pass karta hai yeh kehte hue "teri baari." Agar kid B ek aisi line edit karta hai jo kid A ne likhi thi lekin A ne B ko kabhi us baare mein note nahi pass kiya, toh monitor seeti bajata hai: "Tum dono collide kar sakte ho!" — chahe us din woh actually collide na kiye hon.
# Compile AUR link dono par flag lagao (yeh runtime change karta hai).clang -fsanitize=thread -g -O1 race.c -o race./race # agar is run par race hoti hai toh report print karta hai
clang/gcc ke saath kaam karta hai. Line numbers ke liye -g add karo, -O1 ise fast aur readable rakhta hai.
Runtime cost: ~5–15× slower, ~5–10× zyada memory. Tests/CI par chalao, prod par nahi.
Detect karta hai: data races, deadlocks (lock-order inversions), pthread/std::mutex ka kuch galat istemal, destructors mein use-after-free.
TSAN_OPTIONS=halt_on_error=1 pehli race par rokne ke liye; known-benign waalon ko chup karaane ke liye suppression files.