Sign in to follow this  

synchronize Thread render 3D object and change object Model?

Recommended Posts

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? }

Share this post

Link to post
Share on other sites
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.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this