120 likes | 254 Views
Week 2: Asynchronous Architecture. Game Loop Design. Asynchronous Architecture Synchronous Loop. Quick prototype Complicated simulation code FPS bound Input lags with renderer Non-discrete Physics Only option when not threading Some cell phones & gaming systems
E N D
Week 2: Asynchronous Architecture Game Loop Design
Asynchronous ArchitectureSynchronous Loop • Quick prototype • Complicated simulation code • FPS bound Input lags with renderer • Non-discrete Physics • Only option when not threading • Some cell phones & gaming systems • New O/S’s may have buggy threading
Asynchronous Architecture Asynchronous Loop • Extra setup required • Simplifies Sim via discrete data • Input always represents same delta time • Simulation run at specific time deltas • Performance lost to critical sections • Can use multiple cores
Asynchronous ArchitectureThread Basics • Priority (Normal, Above, High,…) • Extreme Caution: Higher order threads must sleep/wait in order for lower order threads to execute. • Leaving thread kills it. • Desired: Sit thread in a “Wait Event” loop. • Brute Force: Use sleep(1); • ThreadID used to kill it from another.
Asynchronous ArchitectureStandard Thread API • CreateThread() • StartThread() • KillThread()
Asynchronous ArchitectureStandard Thread Use • Creating thread does not start it. • Set Priority (default = normal) • Specify Callback (the thread code) • StartThread() returns immediately • Thread is entered some time later. • Returning from the thread kills it. • Can force kill it from another thread • Normally sit in a “Wait for Event” loop
Asynchronous Architecture Timer Thread Use • Essentially a standard thread • Sits in a Wait event with a timeout • Executes your callback (unless kill raised) • Thread called at specified frequency • Must kill the thread using ThreadID
Asynchronous Architecture Timer Thread Sample • const UINT HZ_32 = 1000/32; • timerID= timeSetEvent( • HZ_32, HZ_32/2, // freq & resolution • TimerThread, // static callback • (DWORD_PTR)this, // UserData • TIME_PERIODIC | TIME_CALLBACK_FUNCTION); • timeKillEvent(timerID);
Asynchronous ArchitectureCross Thread Data Access • volatile • Keyword forces compiler to skip optimizations and read var from RAM. • Mutex (Mutually Exclusive) • Reference counting object that tracks current thread owner. • InterlockedIncrement/Decrement • Inc & store • One CPU operation, no Mutex required
Asynchronous ArchitectureSignals • Event • Raised setEvent( hEvent ); • Not Raised resetEvent( hEvent ); • WaitEvent( hEvent, TimeOut) • Used in a while() loop • Sleeps thread until event raised or timeout
Asynchronous ArchitectureAtom • Cross process data • Simple string • Persists after process shutdown • Process crash can leave data in bad state
Asynchronous ArchitectureSemaphores • Named Semaphores • Use across process boundaries • Better than Atom for use as a Mutex