eratta-check cheytha locking

software enchineeyaringil, loock nedunnathinu munbu locking maanadandam ("loock int") parishodhichu oru loock nedunnathinulla overhead kuraykkaan upayogikkunna oru software design paatten aanu double-check locking ("double-checked locking opttimaiseshan" ennum ariyappedunnu). locking maanadanda parisodhana locking aavasyamaanennu soochippikkukayaanenkil maathrame locking sambhavikkoo.
chila bhashakal/hardware kombineshanukalil eee paatten nadappilaakkumbol athu surakshithamalla. ithu oru virudha maathrkayaayi kanakkaakkanam.
multy-thread parithasthithiyil, prathyekichu singleton paatteninte bhaagamaayi "alasamaaya prarambhavalkkaranam" nadappilaakkumbol, ithu saadhaaranayaayi locking overhead kuraykkaan upayogikkunnu. alasamaaya prarambhavalkkaranam oru moolyam aadyamaayi axus cheiyunnathuvare aarambhikkunnath ozhivaakkunnu.
sii + + 11 ll upayogikkuka.
singleton paattenin, eratta-check cheytha locking aavasyamilla
singleton & gettinstans () {
 staatik singleton ezz;
 s thirike nalkuka;
sii + + 11 umm athinappuravum kokvij _ 1, kokvij _ 2 ennivayude roopathil oru biltt-inn double-check cheytha locking paatten nalkunnu
// singleton.echu.
klaas singleton {
 pothujanangal
 staatik singleton * gettinasttans ();
 swakaarya
 singleton () = sthirasthithi;
 staatik STD:: oopshanal <singleton> s _ instans;
 staatik STD:: vansu _ flag s _ flag;
// singleton.sii. pii. pii.
STD:: oopshanal <singleton> * s _ instans;
STD:: vansu _ flag * s _ flag {};
singleton * * gettinasttans () {
 STD:: kooll _ vansu vansu s _ flag,
 [] () {s _ drishtaantham.emplace (singleton {});});
 thirike & * s _ drishtaantham;
mukalilulla nissaaramaayi pravartthikkunna udaaharanathinu pakaram randuthavana parishodhicha bhaashayaan oraal sarikkum upayogikkaan aagrahikkunnathenkil (udaaharanathinu 2015-leeii releasinu munbulla vishwal studiyo mukalil udharicha kankarant ineeshyalaiseshanekkurichu sii + + 11 staanderdinte bhasha nadappaakkaathathinaal), oraal ettedukkukayum velikal purathirakkukayum cheyyendathundu
klaas singleton {
 pothujanangal
 staatik singleton * gettinasttans ();
 swakaarya
 singleton () = sthirasthithi;
 staatik STD:: aattomik <singleton *> s _ instans;
 staatik estidia mutex ezz _ mutex;
singleton * * gettinasttans () {
 singleton * p = s _ drishtaantham.looad (std:: memory _ oordar _ ackware);
 (p = = nullptr) {// 1st check aanenkil
 STD:: loock _ gaard <STD:: mutex> loock (s _ mutex);
 p = s _ drishtaantham.looad (STD:: memory _ oordar _ relaxed);
 (p = = nullptr) {// 2nd (eratti) aanenkil parisodhikkuka
 p = puthiya singleton ();
 s _ drishtaantham.store (p, std:: memori _ oordar _ release);
 pii thirike nalkuka;
posixil upayogikkuka.
kokvu _ 3 upayogikkanam
library (allengil sub-module) coode athinte epiykku oru samarppita prarambhamaakkal illaathappol aarambikkaan 
nadapadikramam single-thread modil vilikkendathundu.
yaathraykkide upayogikkuka.
paakkej pradhana
samanvayippikkuka" irakkumathi cheyyuka"
vaar aarons samanvayippikkuka.orikkal
vaar aarr [] inttu
// aadya kolil alasathayode aarambikkunna getaar aarr veendedukkunnu. randuthavana parishodhichu
// samanvayippichaanu locking nadappilaakkunnathu.orikkal library pravartthikkunnu. aadyathethu
// doow () ennu vilikkanulla rays vijayikkunnathinulla gorutin shreni aarambikkum, athesamayam
// mattullavar duu () poortthiyaakunnathuvare thadayum. doow oodiyathinushesham, oru
// shreni labhikkunnathin orotta aattomik thaarathamyam aavasyamaanu.
funk gettaar () [] inttu {
 aarons.cheyyuka (funk () {
 aarr thirike nalkuka
funk maine () {
 //randuthavana check cheytha lockingin nandi, rund goruttinukal ()
 //eratta-praarambhathinu kaaranamakilla
 goo gettaar ()
 goo gettaar ()
jaavayil upayogikkuka.
udaaharanathinu, jaava prograaming bhaashayile eee coode segment pariganikkuka (athupole thanne matellaa jaava coode segmentukalum):
// single thread pathipp
klaas phuu {
 swakaarya staatik helper helper;
 pothu sahayiyaaya sahaayikal () {
 (helper = = poojyamaanengil) {
 helper = puthiya helper ();
 sahaiye thirike nalkuka;
 //mattu pravartthanangalum amgangalum...
onniladhikam threadukal upayogikkumbol ithu pravartthikkilla ennathaanu prashnam. rund threadukal oresamayam kokvu _ 4 ennu vilikkunnuvenkil oru loock labhikkanam. allengil, onnukil avar ore samayam object srishtikkaan shramichekkaam, allengil apoornnamaayi aarambikkappetta objectinekkurichulla oru reference oraalkk labhichekkaam.
thaazhepparayunna udaaharanathil kaanichirikkunnathupole, chelaveria samanvayippikkaliloodeyaan loock labhikkunnath.
// sariyaaya ennaal orupakshe chelaveria multithread pathipp
klaas phuu {
 swakaarya sahayiyaaya;
 pothu samanvayippicha sahayiyaaya sahayiyaaya () {
 (helper = = poojyamaanengil) {
 helper = puthiya helper ();
 sahaiye thirike nalkuka;
 //mattu pravartthanangalum amgangalum...
ennirunnaalum, cocks _ 4 lekkulla aadya kooll object srishtikkum, aaa samayat athu axus cheyyaan sramikkunna kurachu threadukal maathrame samanvayippikkendathulla; athinusesham ellah kolukalkkum memper veriyabilinekkurichulla oru reference labikkum.
oru reethi samanvayippikkunnathu chila angeyattatthe kesukalil 100 allengil athil kooduthal ghadakam prakadanam kuraykkaan kaaranamakumennathinaal, eee reethi vilikkumbozhellaam oru loock nedunnathinum release cheiyunnathinumulla overhead anaavasyamaanennu thonnunnu initialisation poorthiyaayikkazhinjaal, loock nedunnathum release cheiyunnathum anaavasyamaanennu thonnunnu. niravadhi programmermar eee sahacharyam inipparayunna reethiyil opttimais cheyyaan sramichittundu
// takarnna multithread pathipp
// "eratta-check cheytha locking" enna bhasha
klaas phuu {
 swakaarya sahayiyaaya;
 pothu sahayiyaaya sahaayikal () {
 (helper = = poojyamaanengil) {
 samanvayippicha (ithu) {
 (helper = = poojyamaanengil) {
 helper = puthiya helper ();
 sahaiye thirike nalkuka;
 //mattu pravartthanangalum amgangalum...
avabodhapoorvvam, rantaim oru veli premitteevu (execution unitukaliludaneelam memory drishyaparatha niyanthrikkunna) undengil eee algoritam prashnathinulla kaaryakshamamaya parihaaramaan, allaathapaksham algoritam ozhivaakkanam. udaaharanathinu, inipparayunna sambhavangalude kramam pariganikkuka
budhimuttaayirikkum 1.4 (muun pathippukal) ll eratta-check cheytha locking upayogikkunnathinte apakadangalilonnu ithu palappozhum pravarthikkunnathaayi thonnunnu ennathaanu saankethikavidyayude sariyaaya nadapakkalum sookshumamaaya prasnangalullathum thammil verthirichariyaan eluppamalla. compiler, schedular threadukal parasparam vidunnathum mattu kankarant sisttam pravartthanatthinte swabhaavavum anusarichu, eratta-check cheytha locking thettaaya reethiyil nadappaakkunnathu moolamundaakunna parajayangal idaykkide maathrame sambhavikkukayulloo. parajayangal punarnirmmikkunnath budhimuttaayirikkum.
budhimuttaayirikkum 5.0 prakaram, eee prashnam pariharichu. asthiramaaya keywade ippol onniladhikam threadukal singleton instans shariyaayi kaikaaryam cheiyunnuvennu urappaakkunnu. eee puthiya bhashyam aandu ennathil vivarichirikkunnu.
// jaava 1.5-lum athinusheshamullavayilum asthirathaykkaayi ackware/release semantixumaayi pravartthikkunnu
// jaava 1.4-nuu keezhilum asthirathaykkulla munkaala ardhashaasthrathin keezhilum thakarthirikkunnu
klaas phuu {
 swakaarya asthira sahayiyaaya sahaayikal;
 pothu sahayiyaaya sahaayikal () {
 helper localreff = helper;
 (localreff = = poojyamaanengil) {
 samanvayippicha (ithu) {
 localreff = helper;
 (localreff = = poojyamaanengil) {
 helper = localreff = puthiya helper ();
 praadeshika refund;
 //mattu pravartthanangalum amgangalum...
anaavashyamaayi thonnunna "local variable" shradhikkuka. ithinte phalam ithinakam aarambikkappetta kesukalil (i.iii.mikkappozhum), asthiramaaya feeld oru thavana maathrame axus cheyyaan kazhiyoo ("pakaram" kaaranam), ithu reethiyude mothatthilulla prakadanam 40 sathamaanam vare mechappeduthaan kazhiyum.
kooduthal budhimuttulla mechanics thudarchayaaya sthiratha nashtappedunnathum (feeld aaksasukal inii samanvayippikkal kramatthil pankedukkilla, asthiramaaya feeldukalilekkulla praveshanathinte aagola kramam) enna chelavil, feeldukal axus cheiyunnathinu relaxed aattomiks upayogikkaan anuvadikkunna klaas jaava 9 avatharippichu, ithu durbalamaaya memory modalukalulla mesheenukalil kurachu vegathil reedukal nalkunnu.
// jaava 9ll avatharippicha warhandlesinaayi ackware/release semantics upayogichu pravartthikkunnu
klaas phuu {
 swakaarya asthira sahayiyaaya sahaayikal;
 pothu sahayiyaaya sahaayikal () {
 helper localreff = gethalperakwayar ();
 (localreff = = poojyamaanengil) {
 samanvayippicha (ithu) {
 localreff = gethalperakwayar ();
 (localreff = = poojyamaanengil) {
 localreff = puthiya helper ();
 sethelper release (localreff);
 praadeshika refund;
 swakaarya staatik final warhandle helper;
 swakaarya sahayiyaaya gethalperakwayar () {
 madakka (sahaayikal) sahaayi.neduka (ithu);
 swakaarya shoonya sethelper release (sahaaya moolyam) {
 sahaayikal.settu release (ithu, moolyam);
 staatik {
 sramikkuka {
 reethi kaikaaryam cheiyunnu.lookup lookup = methed handles.nokkuka ();
 helper = lookup.findwarhandil (phuu.klaas, "helper", helper.klaas);
 } catch (reflective ooppareshan exception iii) {
 puthiya exceptioninicialiser error (iii);
 //mattu pravartthanangalum amgangalum...
helper object staatik aanenkil (oru klaas loderinu onnu), oru badal initialisation-oonn-demand holder bhaashayaan (munbu udharicha vaachakathil ninnu 16.6 listt kaanuka.)
// jaavayil alasamaaya prarambheekaranam shariyaakkuka
klaas phuu {
 swakaarya staatik klaas helperholder {
 public staatik final helper helper = puthiya helper ();
 public staatik helper gethelper () {
 sahaiye thirike nalkuka.sahaayikal;
ithu nested klaasukal reference cheyyappedunnathuvare looad cheyyappedunnilla enna vasthuthaye aasrayichirikkunnu.
thazhepparayunnavayonnum upayogikkaathe helper object surakshithamaayi prasidheekarikkaan jaava 5 leeii feeldinte ardhashaasthram upayogikkaam
public klaas final raap <t> {
 pothu anthima tii moolyam;
 pothu anthima rapper (tii moolyam) {
 ithu.moolyam = moolyam;
public klaas phuu {
 swakaarya final raap <helper> helper raap;
 pothu sahayiyaaya sahaayikal () {
 final raap <helper> temprap = helperrap;
 (tempraper = = poojyamaanengil) {
 samanvayippicha (ithu) {
 (helperrap = = poojyamaanengil) {
 helparraaptar = puthiya finalrapter <helper> (puthiya helper ());
 tempwraper = helparraaptar;
 tempraper thirike nalkuka.moolyam;
krithyathaykku local variable aavasyamaanu jaava memory modalinu keezhil anuvadaneeyamaaya reed reordering kaaranam nall chekkukalum ritten stettmentum upayogikkunnathu paraajayappedaam. eee nadappaakkalinte prakadanam nadappakkunnathinekkaal mikachathaayirikkanamennilla.
sii #ll upayogikkuka.
eratta-check cheytha locking kaaryakshamamaayi nadappakkaan kazhiyum.nettu. singleton nadappaakkalukalil eratta-check cheytha locking cherkkuka ennathaanu oru saadhaarana upayoga reethi
public klaas maisinglettan
 swakaarya staatik object _ mylock = puthiya object ();
 swakaarya staatik maisinglettan _ maisinglettan = poojyamaanu;
 public staatik maisinglettan gettinans ()
 (_ maisinglettan poojyamaanengil)// aadya check
 loock (_ mylock)
 (_ maisinglettan poojyamaanengil)// randaamathe (eratta) check
 _ maisinglettan = puthiya maisinglettan ();
 maisinglettan _ thirike nalkuka;
eee udaaharanathil, poornnamaayum nirmmikkukayum upayogathinu thayyarakukayum cheyyumbol poojyamallaatha maisinglettan objectaanu "loock int".
akathu.nettu framework 4.0, coach _ 6 klaas avatharippichu, ithu nirmmaana samayat erinja apavaadam allengil coach _ 6 lake kaimaria functionte phalam sambharikkunnathin aantharikamaayi sthirasthithiyaayi eratta-check cheytha locking (execution pablikkeshan mode) upayogikkunnu
public klaas maisinglettan
 swakaarya staatik reed onli alassen <maisinglettan> _ maisinglettan = puthiya alassen <maisinglettan> (() => puthiya maisinglettan ());
 public staatik maisinglettan instans => _ maisinglettan.moolyam;

