synchronize Thread render 3D object and change object Model?

Started by
2 comments, last by superpig 18 years, 9 months ago
when we change the 3D at realtime, the thread render must be suspend and wait till our changing finish before resum. i have try and use CRITICAL_SECTION with 2 function EnterCriticalSection and LeaveCriticalSection but it is overlock: myChangeModelFuncton() { EnterCriticalSection(&cs) //-> My Program stuck there model->change(newModel); LeaveCriticalSection(&cs) } myThreadRender() { while(1) { EnterCriticalSection(&cs); model->UpdateRender(); model->render(); LeaveCriticalSection(&cs); } I am very less knowlege of synchronization. can any body suggest me a mechanism to do synchronization in this situation? }
Advertisement
Try using SetEvent-ResetEvent-CreateEvent-WaitFor... APIs. Find some tuts on google for Thread sinhronization, or something like that.

Best,
Zaharije Pasalic
Did you specify the D3DCREATE_MULTITHREADED flag when creating the device?
Robert DunlopThe X-Zonehttp://www.directxzone.org/Microsoft DirectX MVP-------------The opinions expressed in this message are my own personal views and do not reflect the official views of the Microsoft Corporation.The MVP program does not constitute employment or contractual obligation with Microsoft.
Quote:Original post by rdunlop
Did you specify the D3DCREATE_MULTITHREADED flag when creating the device?


You only need to do that if you're actually calling D3D from multiple threads. From what I'm seeing I'd guess he's only calling render functions from one thread, but is performing position/skeleton updates from another.

As far as how to combat this goes: ideally, you want to avoid locks and waits like this whenever possible, keeping a set of data around for the renderer to use at all times. What you can do is multi-buffer your data.

Say you're processing the model's position and orientation. You give the model two sets of positions/orientations, and a flag to track which of the two sets you updated most recently. Your update thread can then check that flag to get a set to read for the old position, update the other set, then toggle the flag (which is an atomic operation - you can't change threads in the middle of a MOV). The renderer can then start by capturing the flag to see which set has been most recently updated, copying that set into local variables, and rendering from it.

That's the basic idea, anyway. It'll probably look a bit jerky but you can improve on it by triple-buffering instead of double-buffering, and then have the renderer 'guess' the current position/orientation by extrapolating from the previous two sets.

There are other approaches too - like having the renderer read from a queue that you push snapshots onto the end of from the update thread.

If you really want to keep the critical section stuff, I suspect that adding a Sleep(0) call after the LeaveCriticalSection call in the renderer thread will fix it. It takes only two or three instructions to process the end of a loop and jump back to the beginning, so myChangeModelFunction only has a two or three instruction window in which to claim the section and do its work. Sleep(0) should ensure that the function is given some processing time within that window.

Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse

This topic is closed to new replies.

Advertisement