{"id":634,"date":"2011-03-29T23:10:05","date_gmt":"2011-03-29T23:10:05","guid":{"rendered":"http:\/\/kera.name\/articles\/?p=634"},"modified":"2011-06-22T09:26:04","modified_gmt":"2011-06-22T09:26:04","slug":"tomalaks-tuesday-tip-11-sleep-your-way-to-success","status":"publish","type":"post","link":"https:\/\/kera.name\/articles\/2011\/03\/tomalaks-tuesday-tip-11-sleep-your-way-to-success\/","title":{"rendered":"Tomalak&#039;s Tuesday Tip #11: Sleep Your Way To Success"},"content":{"rendered":"<p>Tempted to write code like this on Linux?<\/p>\n<p><textarea name=\"code\" class=\"cpp:nocontrols:nogutter\" cols=\"60\" rows=\"10\">#include <ctime><\/p>\n<h1>include <iostream><\/h1>\n<h1>include <unistd.h><\/h1>\n<p>int main()\n{\nstd::time_t target = clock() + (30 * CLOCKS_PER_SEC);\nwhile (clock() < target) {\nusleep(100000);\n}\nstd::cout << \"Done!\";\n\/\/ ^ 30 seconds after program start.. or is it?\n}<\/textarea>\n<\/p>\n<p>Don&#039;t.<\/p>\n<p><a href=\"http:\/\/linux.die.net\/man\/3\/clock\"><code>clock()<\/code><\/a> measures the CPU time taken by the current process, not the absolute passage of time (&#034;wall time&#034;). On most systems, the time spent in a <a href=\"http:\/\/linux.die.net\/man\/3\/sleep\"><code>sleep()<\/code><\/a> or <code>usleep()<\/code> call <em>does not<\/em> count towards the process&#039;s runtime; the OS simply diverts all of its attention to other processes whilst your program waits. The program above could feasibly run for <em>far<\/em> longer than thirty seconds&#8230; perhaps years.<\/p>\n<p>(I&#039;m ignoring here that <code>sleep()<\/code> will actually wait for <em>at least<\/em> the duration you give it; <code>sleep(1)<\/code> is allowed to sleep from anywhere between one second and ten years. Of course, realistically, you&#039;ll get close to a second&#039;s wait on a sane platform. I&#039;m also ignoring that <code>clock()<\/code> wraps around, and will do so every 72 minutes that complies with POSIX by having a <code>CLOCKS_PER_SEC<\/code> value of 1000000.)<\/p>\n<p>You&#039;re better off using a function like plain old <a href=\"http:\/\/linux.die.net\/man\/7\/time\"><code>time()<\/code><\/a> or <a href=\"http:\/\/linux.die.net\/man\/2\/gettimeofday\"><code>gettimeofday()<\/code><\/a> (which can usually give you microseconds&#039; resolution) if you want to measure the wall time:<\/p>\n<p><textarea name=\"code\" class=\"cpp:nocontrols:nogutter\" cols=\"60\" rows=\"10\">#include <ctime><\/p>\n<h1>include <iostream><\/h1>\n<h1>include <unistd.h><\/h1>\n<p>int main()\n{\nstd::time_t target = time(0) + 30;\nwhile (time(0) < target) {\nusleep(100000);\n}\nstd::cout << \"Done!\";\n\/\/ ^ Around 30 seconds after program start.. for real\n}<\/textarea><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Tempted to count the passage of CPU time with the clock() POSIX call? Be careful.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[21,20,33],"_links":{"self":[{"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/posts\/634"}],"collection":[{"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/comments?post=634"}],"version-history":[{"count":8,"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/posts\/634\/revisions"}],"predecessor-version":[{"id":712,"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/posts\/634\/revisions\/712"}],"wp:attachment":[{"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/media?parent=634"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/categories?post=634"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/tags?post=634"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}