Thread, Context Switching and Single Thread programming model.
Multithreading ကိုအရင်က ဒီမှာရေးဖူးပါတယ်
https://web.facebook.com/thet.khine.587/posts/10209979249917378
ဘာလို. multithreading ထားရလဲဆိုတော့ task တွေကို concurrent လုပ်ချင်လို. ဥပမာ web server တွေဆိုရင် ပင်မ web socket ကြီးက နေ incoming request တွေကို လက်ခံတယ် ဒါက main thread ကြီး နောက် ရလာတဲ့ request တခုချင်းဆီကို thread လေးတွေခွဲပြီးတော့ execute လုပ်စေတယ် အဲ့တော့ တပြိုင်နက်တည်းမှာ request ပေါင်းများစွာကို serve လုပ်နိုင်တယ်။
ဒါပေမဲ့ thread မှာလဲ thread creating, context switching ဆိုတဲ့ cost တွေရှိတယ်။ Process တခုကို စ create လုပ်ပြီဆိုတာနဲ. Operating system က process ထဲမှာ အနည်းဆုံး main thread တခုတော့ ထဲ့ပေးရတယ်။ Process တခုတည်းမှာ thread တွေ အများကြီးပါနိုင်တယ်။ ဒါပေမဲ့ operating system အပေါ်မူတည်ပြီးတော့ thread ဘယ်လောက် များများသုံးလို.ရလဲဆိုတာ အကန်.အသတ်ရှိတယ်။
ဆိုပါစို. Word processor ဆိုကြပါစို. စာရိုက်နေတဲ့အတွက် input လက်ခံတာမျိုးက thread တခု. နောက် layout ကို ချဖို.က thread တခု နောက် syntax , grammar မှားသွားရင် စစ်ဖို.က တခြား thread တခု အဲ့လိုလေးတွေ run နေတယ်။
ဒီနေရာမှာ CPU ကို တခုတည်းပဲရှိတယ် multicore မပါဘူးလို.ပဲ စဉ်းစားရအောင်။ အဲ့တော့ ခုနက thread တွေ အများကြီးကို ဘယ်လိုလုပ် တခုတည်းရှိတဲ့ CPU ပေါ်မှာ အပြိုင် run တာလဲပေါ့။ တကယ်က အပြိုင် run လို.ကို မရဘူး ။ဘာလို.ဆိုတော့ CPU တခုသည် တကြိမ်မှာ instruction တခုကိုပဲ run လို.ရမယ်။ Multiple core, multiple processor ဆိုရင်တော့ အပြိုင် run လို.၇မယ်။ ဒါဆိုခုနက single CPU, single core ကိစ္စ မှာ thread တွေကို အပြိုင် လိုမြင်ရအောင် ဘယ်လို run လဲဆိုရင် များသောအားဖြင့် time slicing ဆိုတာကိုသုံးပြီး run တယ်။
time slicing ဆိုတာက ဘာလဲဆိုတော့ ခုနေ thread 1 ကိုအလှည့်ပေးလိုက်တယ်။ နောက် thread 2 ကိုအလှည့်ပေးမယ် thread တွေကြားထဲမှာ တယောက် နည်းနည်း နည်းနည်း အချိန်လှည့်ပေးပြီး run ရတာကိုပြောတာ။ ဘယ်ကောင်ပြီးရင် ဘယ်ကောင် run မယ် priority ကျတော့ဘယ်သူ.ပေးမယ်ဆိုတာ thread scheduling algorithm အပေါ်မူတည်တယ်။
အဲ့သလိုခုနက thread တခုချင်းသည် အပြိုင် run လို.ရအောင် ဘယ်သူက ဘယ်နေရာမှာ run နေတယ် ဘယ်လို code နေရာရောက်နေတယ်။ အဲ့တာတွေ မှတ်ဖို.လိုတယ်။ နောက် calling stack, ဘယ် thread က ဘယ် method ကို run နေတာ သူနဲ.ဆိုင်တဲ့ memory တွေ လိုတယ် ။ အဓိက call stack လိုတယ်။ နောက် register state တွေလိုတယ်။ ဒါမှ ဒီ thread အလှည့်ပြီးလို. နောက် thread ကို အလှည့်ပေးရင် သူ.မှာလောလောဆယ် သုံးနေတဲ့ thread state ကို သိမ်းထားလို.ရအောင်လို.။ နောက် thread တွေသည် တူညီတဲ့ code , method တွေကို ၀င် run နိုင်တဲ့အတွက် conflict မဖြစ်အောင် concurrency ကိုထိန်းရတယ် အဲ့အတွက် locking mechanism တွေလိုတယ်။ ခုနကပြောတဲ့အချက်တွေသည် thread create လုပ်တိုင်းမှာလိုတယ်။
နောက်တခုက thread 1 အလှည့်ပြီးလို. thread 2 အလှည့် run မယ် ဒါဆိုရင် thread 2 မင်း run တော့လို. ဆိုတာနဲ.မပြီးဘူး။ ဘာလုပ်ရသလဲဆိုတော့ thread 1 ရဲ. thread state, register value တွေကို thread 1 ရဲ. thread space မှာသိမ်းရတယ်။ call stack တွေကို save ရတယ်။ နောက် thread 2 ရဲ. call stack , heap address , register value, တွေ ပြန် ထဲ့ရတယ် ။ instruction pointer ကို ပြန်ထဲ့ရတယ်။ အဲ့တာလုပ်ရတာကို context switch လို.ခေါ်တယ်။
စဉ်းစားကြည့်ရင် thread များလာမယ်ဆိုရင် တယောက် ကိုတယောက် အလှည့်ပေးရလွန်းတော့ context switch ခနခနလုပ်ရမယ်။ အဲ့တော့ CPU က တကယ့်အလုပ်ကို မလုပ်ရပဲနဲ. thread management မှာပဲ အချိန်ကုန်သလိုဖြစ်နေတယ်။
ဒါမျိုးကိုရှင်းထုတ်ဖို. Node.js မှာကျတော့ Single thread ကိုသုံးလိုက်တယ်။ ဒါပေမဲ့ Main thread တခုတည်းဆိုပေမဲ့ file read တာလို IO မျိုးကျတော့ libuv ကိုသုံးရတယ်။ libuv သည် internally မှာ thread 4 ခုလောက်ခွဲပြီးလုပ်တယ်။ thread ကို polling လုပ်ပြီး လှည့်သုံးတယ်။
အောက်က ကောင်က non blocking, blocking IO ကိုရှင်းထားတာ.
https://web.facebook.com/thet.khine.587/posts/10209973224766753
Original link =>(https://m.facebook.com/story.php?story_fbid=pfbid02RMmNRVuKPR8c5Xdfp2WKjuXsokKzqxmQjJra6kU9Jf317ZPVLp6z86S7XUBjz9Fpl&id=1819241055&mibextid=Nif5oz)