Implementing time delay function in C -


i want implement delay function using null loops. amount of time needed complete loop once compiler , machine dependant. want program determine time on own , delay program specified amount of time. can give me idea how this? n. b. there function named delay() suspends system specified milliseconds. possible suspend system without using function?

first of all, should never sit in loop doing nothing. not waste energy (as keeps cpu 100% busy counting loop counter) -- in multitasking system decreases whole system performance, because process getting time slices time appears doing something.

next point ... don't know of delay() function. not standard c. in fact, until c11, there no standard @ things this.

posix rescue, there usleep(3) (deprecated) , nanosleep(2). if you're on posix-compliant system, you'll fine those. block (means, scheduler of os knows have nothing , schedules them after end of call), don't waste cpu power.

if you're on windows, direct delay in code, have sleep(). note function takes milliseconds, has precision around 15ms. enough, not always. if need better precision on windows, can request more timer interrupts using timebeginperiod() ... timebeginperiod(1); request timer interrupt each millisecond. don't forget calling timeendperiod() the same value don't need precision more, because more timer interrupts come cost: keep system busy, wasting more energy.

i had similar problem developing little game recently, needed constant ticks in 10ms intervals, came for posix-compliant systems , for windows. ticker_wait() function in code suspends until next tick, maybe helpful if original intent timing issue.


Comments

Popular posts from this blog

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -

YouTubePlayerFragment cannot be cast to android.support.v4.app.Fragment -