[{"data":1,"prerenderedAt":1021},["ShallowReactive",2],{"article_list_timing_":3},[4],{"_path":5,"_dir":6,"_draft":7,"_partial":7,"_locale":8,"title":9,"description":10,"tags":11,"image":14,"publishDate":6,"excerpt":10,"body":15,"_type":1012,"_id":1013,"_source":1014,"_file":1015,"_stem":1016,"_extension":1017,"author":1018},"/jbagley/2021-08-01/accuratetiming","2021-08-01",false,"","Accurate Timing","In many tasks we need to do something at given intervals of time. The most obvious ways may not give you the best results.",[12,13],"c++","timing","/jbagley/2021-08-01/img/accurateTiming.jpg",{"type":16,"children":17,"toc":1005},"root",[18,25,32,46,185,208,213,219,224,244,881,900,913,918,928,941,975,987,999],{"type":19,"tag":20,"props":21,"children":22},"element","p",{},[23],{"type":24,"value":10},"text",{"type":19,"tag":26,"props":27,"children":29},"h2",{"id":28},"time-meh",[30],{"type":24,"value":31},"Time? Meh.",{"type":19,"tag":20,"props":33,"children":34},{},[35,37,44],{"type":24,"value":36},"The most basic tasks that don't have what you might call CPU-scale time\nrequirements can be handled with the usual language and framework features. These\ncan be things like timers, or simply a call to a ",{"type":19,"tag":38,"props":39,"children":41},"code",{"className":40},[],[42],{"type":24,"value":43},"sleep(number_of_milliseconds)",{"type":24,"value":45}," function.",{"type":19,"tag":47,"props":48,"children":52},"pre",{"className":49,"code":50,"language":51,"meta":8,"style":8},"language-cpp shiki shiki-themes github-light github-dark","void RunTimedThing()\n{\n   while (true)\n   {\n      DoMyThing();\n      sleep(1);      // wake up about 1 millisecond later to do it again.\n   }\n}\n","cpp",[53],{"type":19,"tag":38,"props":54,"children":55},{"__ignoreMap":8},[56,80,89,114,123,137,167,176],{"type":19,"tag":57,"props":58,"children":61},"span",{"class":59,"line":60},"line",1,[62,68,74],{"type":19,"tag":57,"props":63,"children":65},{"style":64},"--shiki-default:#D73A49;--shiki-dark:#F97583",[66],{"type":24,"value":67},"void",{"type":19,"tag":57,"props":69,"children":71},{"style":70},"--shiki-default:#6F42C1;--shiki-dark:#B392F0",[72],{"type":24,"value":73}," RunTimedThing",{"type":19,"tag":57,"props":75,"children":77},{"style":76},"--shiki-default:#24292E;--shiki-dark:#E1E4E8",[78],{"type":24,"value":79},"()\n",{"type":19,"tag":57,"props":81,"children":83},{"class":59,"line":82},2,[84],{"type":19,"tag":57,"props":85,"children":86},{"style":76},[87],{"type":24,"value":88},"{\n",{"type":19,"tag":57,"props":90,"children":92},{"class":59,"line":91},3,[93,98,103,109],{"type":19,"tag":57,"props":94,"children":95},{"style":64},[96],{"type":24,"value":97},"   while",{"type":19,"tag":57,"props":99,"children":100},{"style":76},[101],{"type":24,"value":102}," (",{"type":19,"tag":57,"props":104,"children":106},{"style":105},"--shiki-default:#005CC5;--shiki-dark:#79B8FF",[107],{"type":24,"value":108},"true",{"type":19,"tag":57,"props":110,"children":111},{"style":76},[112],{"type":24,"value":113},")\n",{"type":19,"tag":57,"props":115,"children":117},{"class":59,"line":116},4,[118],{"type":19,"tag":57,"props":119,"children":120},{"style":76},[121],{"type":24,"value":122},"   {\n",{"type":19,"tag":57,"props":124,"children":126},{"class":59,"line":125},5,[127,132],{"type":19,"tag":57,"props":128,"children":129},{"style":70},[130],{"type":24,"value":131},"      DoMyThing",{"type":19,"tag":57,"props":133,"children":134},{"style":76},[135],{"type":24,"value":136},"();\n",{"type":19,"tag":57,"props":138,"children":140},{"class":59,"line":139},6,[141,146,151,156,161],{"type":19,"tag":57,"props":142,"children":143},{"style":70},[144],{"type":24,"value":145},"      sleep",{"type":19,"tag":57,"props":147,"children":148},{"style":76},[149],{"type":24,"value":150},"(",{"type":19,"tag":57,"props":152,"children":153},{"style":105},[154],{"type":24,"value":155},"1",{"type":19,"tag":57,"props":157,"children":158},{"style":76},[159],{"type":24,"value":160},");",{"type":19,"tag":57,"props":162,"children":164},{"style":163},"--shiki-default:#6A737D;--shiki-dark:#6A737D",[165],{"type":24,"value":166},"      // wake up about 1 millisecond later to do it again.\n",{"type":19,"tag":57,"props":168,"children":170},{"class":59,"line":169},7,[171],{"type":19,"tag":57,"props":172,"children":173},{"style":76},[174],{"type":24,"value":175},"   }\n",{"type":19,"tag":57,"props":177,"children":179},{"class":59,"line":178},8,[180],{"type":19,"tag":57,"props":181,"children":182},{"style":76},[183],{"type":24,"value":184},"}\n",{"type":19,"tag":186,"props":187,"children":188},"blockquote",{},[189],{"type":19,"tag":20,"props":190,"children":191},{},[192,198,200,206],{"type":19,"tag":193,"props":194,"children":195},"strong",{},[196],{"type":24,"value":197},"Exercise 1",{"type":24,"value":199},": How much time is there between each call to ",{"type":19,"tag":38,"props":201,"children":203},{"className":202},[],[204],{"type":24,"value":205},"DoMyThing()",{"type":24,"value":207},"?",{"type":19,"tag":20,"props":209,"children":210},{},[211],{"type":24,"value":212},"Most OS thread scheduling is not guaranteed, so your thread isn't usually\nexecuted again exactly when you want it to be. If you are fine with your thing\nbeing done again not exactly 1 ms later, this approach is acceptable. But if you\nneed accuracy to the millisecond, or you need it to run more frequently than 1\nms, it is time to level up.",{"type":19,"tag":26,"props":214,"children":216},{"id":215},"when-it-positively-absolutely-has-to-be-done-on-time",[217],{"type":24,"value":218},"When it positively, absolutely has to be done on time",{"type":19,"tag":20,"props":220,"children":221},{},[222],{"type":24,"value":223},"Now you will need a high resolution timer. These have millisecond or even\nnanosecond accuracy. It used to be more of a _maybe-you-can-maybe-you-can't kind\nof feature feature, but with modern hardware, even the small guys, you usually\nwill be able to get all the way up to nanosecond precision.",{"type":19,"tag":20,"props":225,"children":226},{},[227,229,242],{"type":24,"value":228},"For C++, ",{"type":19,"tag":230,"props":231,"children":235},"a",{"href":232,"rel":233},"https://en.cppreference.com/w/cpp/chrono",[234],"nofollow",[236],{"type":19,"tag":38,"props":237,"children":239},{"className":238},[],[240],{"type":24,"value":241},"std::chrono",{"type":24,"value":243}," provides such facilities.",{"type":19,"tag":47,"props":245,"children":247},{"className":49,"code":246,"language":51,"meta":8,"style":8},"using namespace std::chrono_literals;              // For using 1ms as a const literal for 1 millisecond\nusing namespace std::chrono;                       // For the sake of brevity and sanity\nusing Clock = std::chrono::high_resolution_clock;  // For ease in changing our timing resolution\n\nvoid RunTimedThingAccurately()\n{\n   const auto kMyThingFrequency = 1ms;\n   const auto kMaxSleepTime = kMyThingFrequency / 2.0; // set to something less than the frequency\n\n   auto nextTime = Clock::now();\n   while (true)\n   {\n      const auto currentTime = Clock::now();\n      const auto remainingTime = duration_cast\u003Cmicroseconds>(nextTime - currentTime);\n      if (remainingTime.count() \u003C= 0LL)\n      {\n         DoMyThing();\n         nextTime = currentTime + kMyThingFrequency + remainingTime;\n         sleepTime = maxSleepTime;\n      }\n      else\n      {\n         sleepTime = std::min(maxSleepTime, remainingTime);\n      }\n  \n      std::this_thread::sleep_for(remainingTime);\n   }\n}\n",[248],{"type":19,"tag":38,"props":249,"children":250},{"__ignoreMap":8},[251,289,322,369,378,394,401,439,482,490,525,545,553,591,642,685,694,707,742,760,769,778,786,816,824,833,865,873],{"type":19,"tag":57,"props":252,"children":253},{"class":59,"line":60},[254,259,264,269,274,279,284],{"type":19,"tag":57,"props":255,"children":256},{"style":64},[257],{"type":24,"value":258},"using",{"type":19,"tag":57,"props":260,"children":261},{"style":64},[262],{"type":24,"value":263}," namespace",{"type":19,"tag":57,"props":265,"children":266},{"style":70},[267],{"type":24,"value":268}," std",{"type":19,"tag":57,"props":270,"children":271},{"style":76},[272],{"type":24,"value":273},"::",{"type":19,"tag":57,"props":275,"children":276},{"style":70},[277],{"type":24,"value":278},"chrono_literals",{"type":19,"tag":57,"props":280,"children":281},{"style":76},[282],{"type":24,"value":283},";",{"type":19,"tag":57,"props":285,"children":286},{"style":163},[287],{"type":24,"value":288},"              // For using 1ms as a const literal for 1 millisecond\n",{"type":19,"tag":57,"props":290,"children":291},{"class":59,"line":82},[292,296,300,304,308,313,317],{"type":19,"tag":57,"props":293,"children":294},{"style":64},[295],{"type":24,"value":258},{"type":19,"tag":57,"props":297,"children":298},{"style":64},[299],{"type":24,"value":263},{"type":19,"tag":57,"props":301,"children":302},{"style":70},[303],{"type":24,"value":268},{"type":19,"tag":57,"props":305,"children":306},{"style":76},[307],{"type":24,"value":273},{"type":19,"tag":57,"props":309,"children":310},{"style":70},[311],{"type":24,"value":312},"chrono",{"type":19,"tag":57,"props":314,"children":315},{"style":76},[316],{"type":24,"value":283},{"type":19,"tag":57,"props":318,"children":319},{"style":163},[320],{"type":24,"value":321},"                       // For the sake of brevity and sanity\n",{"type":19,"tag":57,"props":323,"children":324},{"class":59,"line":91},[325,329,334,339,343,347,351,355,360,364],{"type":19,"tag":57,"props":326,"children":327},{"style":64},[328],{"type":24,"value":258},{"type":19,"tag":57,"props":330,"children":331},{"style":70},[332],{"type":24,"value":333}," Clock",{"type":19,"tag":57,"props":335,"children":336},{"style":64},[337],{"type":24,"value":338}," =",{"type":19,"tag":57,"props":340,"children":341},{"style":70},[342],{"type":24,"value":268},{"type":19,"tag":57,"props":344,"children":345},{"style":76},[346],{"type":24,"value":273},{"type":19,"tag":57,"props":348,"children":349},{"style":70},[350],{"type":24,"value":312},{"type":19,"tag":57,"props":352,"children":353},{"style":76},[354],{"type":24,"value":273},{"type":19,"tag":57,"props":356,"children":357},{"style":70},[358],{"type":24,"value":359},"high_resolution_clock",{"type":19,"tag":57,"props":361,"children":362},{"style":76},[363],{"type":24,"value":283},{"type":19,"tag":57,"props":365,"children":366},{"style":163},[367],{"type":24,"value":368},"  // For ease in changing our timing resolution\n",{"type":19,"tag":57,"props":370,"children":371},{"class":59,"line":116},[372],{"type":19,"tag":57,"props":373,"children":375},{"emptyLinePlaceholder":374},true,[376],{"type":24,"value":377},"\n",{"type":19,"tag":57,"props":379,"children":380},{"class":59,"line":125},[381,385,390],{"type":19,"tag":57,"props":382,"children":383},{"style":64},[384],{"type":24,"value":67},{"type":19,"tag":57,"props":386,"children":387},{"style":70},[388],{"type":24,"value":389}," RunTimedThingAccurately",{"type":19,"tag":57,"props":391,"children":392},{"style":76},[393],{"type":24,"value":79},{"type":19,"tag":57,"props":395,"children":396},{"class":59,"line":139},[397],{"type":19,"tag":57,"props":398,"children":399},{"style":76},[400],{"type":24,"value":88},{"type":19,"tag":57,"props":402,"children":403},{"class":59,"line":169},[404,409,414,419,424,429,434],{"type":19,"tag":57,"props":405,"children":406},{"style":64},[407],{"type":24,"value":408},"   const",{"type":19,"tag":57,"props":410,"children":411},{"style":64},[412],{"type":24,"value":413}," auto",{"type":19,"tag":57,"props":415,"children":416},{"style":76},[417],{"type":24,"value":418}," kMyThingFrequency ",{"type":19,"tag":57,"props":420,"children":421},{"style":64},[422],{"type":24,"value":423},"=",{"type":19,"tag":57,"props":425,"children":426},{"style":105},[427],{"type":24,"value":428}," 1",{"type":19,"tag":57,"props":430,"children":431},{"style":64},[432],{"type":24,"value":433},"ms",{"type":19,"tag":57,"props":435,"children":436},{"style":76},[437],{"type":24,"value":438},";\n",{"type":19,"tag":57,"props":440,"children":441},{"class":59,"line":178},[442,446,450,455,459,463,468,473,477],{"type":19,"tag":57,"props":443,"children":444},{"style":64},[445],{"type":24,"value":408},{"type":19,"tag":57,"props":447,"children":448},{"style":64},[449],{"type":24,"value":413},{"type":19,"tag":57,"props":451,"children":452},{"style":76},[453],{"type":24,"value":454}," kMaxSleepTime ",{"type":19,"tag":57,"props":456,"children":457},{"style":64},[458],{"type":24,"value":423},{"type":19,"tag":57,"props":460,"children":461},{"style":76},[462],{"type":24,"value":418},{"type":19,"tag":57,"props":464,"children":465},{"style":64},[466],{"type":24,"value":467},"/",{"type":19,"tag":57,"props":469,"children":470},{"style":105},[471],{"type":24,"value":472}," 2.0",{"type":19,"tag":57,"props":474,"children":475},{"style":76},[476],{"type":24,"value":283},{"type":19,"tag":57,"props":478,"children":479},{"style":163},[480],{"type":24,"value":481}," // set to something less than the frequency\n",{"type":19,"tag":57,"props":483,"children":485},{"class":59,"line":484},9,[486],{"type":19,"tag":57,"props":487,"children":488},{"emptyLinePlaceholder":374},[489],{"type":24,"value":377},{"type":19,"tag":57,"props":491,"children":493},{"class":59,"line":492},10,[494,499,504,508,512,516,521],{"type":19,"tag":57,"props":495,"children":496},{"style":64},[497],{"type":24,"value":498},"   auto",{"type":19,"tag":57,"props":500,"children":501},{"style":76},[502],{"type":24,"value":503}," nextTime ",{"type":19,"tag":57,"props":505,"children":506},{"style":64},[507],{"type":24,"value":423},{"type":19,"tag":57,"props":509,"children":510},{"style":70},[511],{"type":24,"value":333},{"type":19,"tag":57,"props":513,"children":514},{"style":76},[515],{"type":24,"value":273},{"type":19,"tag":57,"props":517,"children":518},{"style":70},[519],{"type":24,"value":520},"now",{"type":19,"tag":57,"props":522,"children":523},{"style":76},[524],{"type":24,"value":136},{"type":19,"tag":57,"props":526,"children":528},{"class":59,"line":527},11,[529,533,537,541],{"type":19,"tag":57,"props":530,"children":531},{"style":64},[532],{"type":24,"value":97},{"type":19,"tag":57,"props":534,"children":535},{"style":76},[536],{"type":24,"value":102},{"type":19,"tag":57,"props":538,"children":539},{"style":105},[540],{"type":24,"value":108},{"type":19,"tag":57,"props":542,"children":543},{"style":76},[544],{"type":24,"value":113},{"type":19,"tag":57,"props":546,"children":548},{"class":59,"line":547},12,[549],{"type":19,"tag":57,"props":550,"children":551},{"style":76},[552],{"type":24,"value":122},{"type":19,"tag":57,"props":554,"children":556},{"class":59,"line":555},13,[557,562,566,571,575,579,583,587],{"type":19,"tag":57,"props":558,"children":559},{"style":64},[560],{"type":24,"value":561},"      const",{"type":19,"tag":57,"props":563,"children":564},{"style":64},[565],{"type":24,"value":413},{"type":19,"tag":57,"props":567,"children":568},{"style":76},[569],{"type":24,"value":570}," currentTime ",{"type":19,"tag":57,"props":572,"children":573},{"style":64},[574],{"type":24,"value":423},{"type":19,"tag":57,"props":576,"children":577},{"style":70},[578],{"type":24,"value":333},{"type":19,"tag":57,"props":580,"children":581},{"style":76},[582],{"type":24,"value":273},{"type":19,"tag":57,"props":584,"children":585},{"style":70},[586],{"type":24,"value":520},{"type":19,"tag":57,"props":588,"children":589},{"style":76},[590],{"type":24,"value":136},{"type":19,"tag":57,"props":592,"children":594},{"class":59,"line":593},14,[595,599,603,608,612,617,622,627,632,637],{"type":19,"tag":57,"props":596,"children":597},{"style":64},[598],{"type":24,"value":561},{"type":19,"tag":57,"props":600,"children":601},{"style":64},[602],{"type":24,"value":413},{"type":19,"tag":57,"props":604,"children":605},{"style":76},[606],{"type":24,"value":607}," remainingTime ",{"type":19,"tag":57,"props":609,"children":610},{"style":64},[611],{"type":24,"value":423},{"type":19,"tag":57,"props":613,"children":614},{"style":70},[615],{"type":24,"value":616}," duration_cast",{"type":19,"tag":57,"props":618,"children":619},{"style":76},[620],{"type":24,"value":621},"\u003C",{"type":19,"tag":57,"props":623,"children":624},{"style":70},[625],{"type":24,"value":626},"microseconds",{"type":19,"tag":57,"props":628,"children":629},{"style":76},[630],{"type":24,"value":631},">(nextTime ",{"type":19,"tag":57,"props":633,"children":634},{"style":64},[635],{"type":24,"value":636},"-",{"type":19,"tag":57,"props":638,"children":639},{"style":76},[640],{"type":24,"value":641}," currentTime);\n",{"type":19,"tag":57,"props":643,"children":645},{"class":59,"line":644},15,[646,651,656,661,666,671,676,681],{"type":19,"tag":57,"props":647,"children":648},{"style":64},[649],{"type":24,"value":650},"      if",{"type":19,"tag":57,"props":652,"children":653},{"style":76},[654],{"type":24,"value":655}," (remainingTime.",{"type":19,"tag":57,"props":657,"children":658},{"style":70},[659],{"type":24,"value":660},"count",{"type":19,"tag":57,"props":662,"children":663},{"style":76},[664],{"type":24,"value":665},"() ",{"type":19,"tag":57,"props":667,"children":668},{"style":64},[669],{"type":24,"value":670},"\u003C=",{"type":19,"tag":57,"props":672,"children":673},{"style":105},[674],{"type":24,"value":675}," 0",{"type":19,"tag":57,"props":677,"children":678},{"style":64},[679],{"type":24,"value":680},"LL",{"type":19,"tag":57,"props":682,"children":683},{"style":76},[684],{"type":24,"value":113},{"type":19,"tag":57,"props":686,"children":688},{"class":59,"line":687},16,[689],{"type":19,"tag":57,"props":690,"children":691},{"style":76},[692],{"type":24,"value":693},"      {\n",{"type":19,"tag":57,"props":695,"children":697},{"class":59,"line":696},17,[698,703],{"type":19,"tag":57,"props":699,"children":700},{"style":70},[701],{"type":24,"value":702},"         DoMyThing",{"type":19,"tag":57,"props":704,"children":705},{"style":76},[706],{"type":24,"value":136},{"type":19,"tag":57,"props":708,"children":710},{"class":59,"line":709},18,[711,716,720,724,729,733,737],{"type":19,"tag":57,"props":712,"children":713},{"style":76},[714],{"type":24,"value":715},"         nextTime ",{"type":19,"tag":57,"props":717,"children":718},{"style":64},[719],{"type":24,"value":423},{"type":19,"tag":57,"props":721,"children":722},{"style":76},[723],{"type":24,"value":570},{"type":19,"tag":57,"props":725,"children":726},{"style":64},[727],{"type":24,"value":728},"+",{"type":19,"tag":57,"props":730,"children":731},{"style":76},[732],{"type":24,"value":418},{"type":19,"tag":57,"props":734,"children":735},{"style":64},[736],{"type":24,"value":728},{"type":19,"tag":57,"props":738,"children":739},{"style":76},[740],{"type":24,"value":741}," remainingTime;\n",{"type":19,"tag":57,"props":743,"children":745},{"class":59,"line":744},19,[746,751,755],{"type":19,"tag":57,"props":747,"children":748},{"style":76},[749],{"type":24,"value":750},"         sleepTime ",{"type":19,"tag":57,"props":752,"children":753},{"style":64},[754],{"type":24,"value":423},{"type":19,"tag":57,"props":756,"children":757},{"style":76},[758],{"type":24,"value":759}," maxSleepTime;\n",{"type":19,"tag":57,"props":761,"children":763},{"class":59,"line":762},20,[764],{"type":19,"tag":57,"props":765,"children":766},{"style":76},[767],{"type":24,"value":768},"      }\n",{"type":19,"tag":57,"props":770,"children":772},{"class":59,"line":771},21,[773],{"type":19,"tag":57,"props":774,"children":775},{"style":64},[776],{"type":24,"value":777},"      else\n",{"type":19,"tag":57,"props":779,"children":781},{"class":59,"line":780},22,[782],{"type":19,"tag":57,"props":783,"children":784},{"style":76},[785],{"type":24,"value":693},{"type":19,"tag":57,"props":787,"children":789},{"class":59,"line":788},23,[790,794,798,802,806,811],{"type":19,"tag":57,"props":791,"children":792},{"style":76},[793],{"type":24,"value":750},{"type":19,"tag":57,"props":795,"children":796},{"style":64},[797],{"type":24,"value":423},{"type":19,"tag":57,"props":799,"children":800},{"style":70},[801],{"type":24,"value":268},{"type":19,"tag":57,"props":803,"children":804},{"style":76},[805],{"type":24,"value":273},{"type":19,"tag":57,"props":807,"children":808},{"style":70},[809],{"type":24,"value":810},"min",{"type":19,"tag":57,"props":812,"children":813},{"style":76},[814],{"type":24,"value":815},"(maxSleepTime, remainingTime);\n",{"type":19,"tag":57,"props":817,"children":819},{"class":59,"line":818},24,[820],{"type":19,"tag":57,"props":821,"children":822},{"style":76},[823],{"type":24,"value":768},{"type":19,"tag":57,"props":825,"children":827},{"class":59,"line":826},25,[828],{"type":19,"tag":57,"props":829,"children":830},{"style":76},[831],{"type":24,"value":832},"  \n",{"type":19,"tag":57,"props":834,"children":836},{"class":59,"line":835},26,[837,842,846,851,855,860],{"type":19,"tag":57,"props":838,"children":839},{"style":70},[840],{"type":24,"value":841},"      std",{"type":19,"tag":57,"props":843,"children":844},{"style":76},[845],{"type":24,"value":273},{"type":19,"tag":57,"props":847,"children":848},{"style":70},[849],{"type":24,"value":850},"this_thread",{"type":19,"tag":57,"props":852,"children":853},{"style":76},[854],{"type":24,"value":273},{"type":19,"tag":57,"props":856,"children":857},{"style":70},[858],{"type":24,"value":859},"sleep_for",{"type":19,"tag":57,"props":861,"children":862},{"style":76},[863],{"type":24,"value":864},"(remainingTime);\n",{"type":19,"tag":57,"props":866,"children":868},{"class":59,"line":867},27,[869],{"type":19,"tag":57,"props":870,"children":871},{"style":76},[872],{"type":24,"value":175},{"type":19,"tag":57,"props":874,"children":876},{"class":59,"line":875},28,[877],{"type":19,"tag":57,"props":878,"children":879},{"style":76},[880],{"type":24,"value":184},{"type":19,"tag":186,"props":882,"children":883},{},[884],{"type":19,"tag":20,"props":885,"children":886},{},[887,892,894,899],{"type":19,"tag":193,"props":888,"children":889},{},[890],{"type":24,"value":891},"Exercise 2",{"type":24,"value":893},": Now how much time is there between each call to ",{"type":19,"tag":38,"props":895,"children":897},{"className":896},[],[898],{"type":24,"value":205},{"type":24,"value":207},{"type":19,"tag":20,"props":901,"children":902},{},[903,905,911],{"type":24,"value":904},"Notice that it still isn't enough to just sleep for the interval at which you\nwant to do your thing. Even the higher resolution will have some slop, called\njitter, around its timings. To combat that we do a little polling, waking up to\nsee if it is time then sleeping for the remainder of the time until we do our\nthing. You can try to optimize ",{"type":19,"tag":38,"props":906,"children":908},{"className":907},[],[909],{"type":24,"value":910},"kMaxSleepTime",{"type":24,"value":912}," to improve accuracy and minimize\ntimes being awakened before the work needs to be done.",{"type":19,"tag":20,"props":914,"children":915},{},[916],{"type":24,"value":917},"You may have noticed this subtlety, it is where some magic happens",{"type":19,"tag":47,"props":919,"children":923},{"className":920,"code":922,"language":24},[921],"language-text","nextTime = currentTime + kMyThingFrequency + remainingTime;\n",[924],{"type":19,"tag":38,"props":925,"children":926},{"__ignoreMap":8},[927],{"type":24,"value":922},{"type":19,"tag":20,"props":929,"children":930},{},[931,933,939],{"type":24,"value":932},"The remaining time will be negative or zero when this block executes, so it\nautomatically handles being woken up a little late. Otherwise, ",{"type":19,"tag":38,"props":934,"children":936},{"className":935},[],[937],{"type":24,"value":938},"nextTime",{"type":24,"value":940}," would\naccumulate those errors.",{"type":19,"tag":20,"props":942,"children":943},{},[944,946,951,953,958,960,966,968,973],{"type":24,"value":945},"Another subtlety about ",{"type":19,"tag":38,"props":947,"children":949},{"className":948},[],[950],{"type":24,"value":910},{"type":24,"value":952},". ",{"type":19,"tag":38,"props":954,"children":956},{"className":955},[],[957],{"type":24,"value":205},{"type":24,"value":959}," obviously must take less\ntime than ",{"type":19,"tag":38,"props":961,"children":963},{"className":962},[],[964],{"type":24,"value":965},"kMyThingFrequency",{"type":24,"value":967},". How long it takes will influence the value you\nassign to ",{"type":19,"tag":38,"props":969,"children":971},{"className":970},[],[972],{"type":24,"value":910},{"type":24,"value":974}," which needs to be less than the difference between how\nlong your thing takes and how often your thing needs to happen.",{"type":19,"tag":976,"props":977,"children":979},"h3",{"id":978},"a-note-on-stdchrono",[980,982],{"type":24,"value":981},"A note on ",{"type":19,"tag":38,"props":983,"children":985},{"className":984},[],[986],{"type":24,"value":241},{"type":19,"tag":20,"props":988,"children":989},{},[990,992,997],{"type":24,"value":991},"The ",{"type":19,"tag":38,"props":993,"children":995},{"className":994},[],[996],{"type":24,"value":241},{"type":24,"value":998}," code can be a little ugly without the aliases. But the unit\nhandling is superb. Once you are used to it you may wish to do similar things\nfor other values with units. It is well worth getting comfortable with it\nwhen you have to do any time based calculations.",{"type":19,"tag":1000,"props":1001,"children":1002},"style",{},[1003],{"type":24,"value":1004},"html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":8,"searchDepth":91,"depth":91,"links":1006},[1007,1008],{"id":28,"depth":82,"text":31},{"id":215,"depth":82,"text":218,"children":1009},[1010],{"id":978,"depth":91,"text":1011},"A note on std::chrono","markdown","content:jbagley:2021-08-01:AccurateTiming.md","content","jbagley/2021-08-01/AccurateTiming.md","jbagley/2021-08-01/AccurateTiming","md",{"user":1019,"name":1020},"jbagley","Jason Bagley",1780330268512]