Jump to content
  • Advertisement
Sign in to follow this  
fathom88

Question About Synchronization

This topic is 4459 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a question about synchronization. I get streaming data. Before I can render the data int an image, I must process the data. I want to alert my render window that a frame is ready to be drawn to the screen. Normally, I would simply have a boolean flag. I would have the data processing thread set a semaphore and have the render thread do a WaitOnObject for the semaphore. My problem is I have multiple rendering windows. When I tried the same method, one window would render but the others would do nothing. The render window that got the signal 1st would draw correctly, but the others failed notice new data was ready for drawing. Any suggestions on how would fix this problem? There's probably a simple solution that I'm simply not seeing. Thanks in advance for any help.

Share this post


Link to post
Share on other sites
Advertisement
sounds like you want a conditional variable instead of a semaphore.

everytime you signal a conditional variable, one waiting thread is awakened, so you can have rendering threads wait on the cond. variable, then have the update thread signal the cond. variable as many times as you have rendering threads.

Share this post


Link to post
Share on other sites
CreateEvent RenderEvent with bManualReset to TRUE.
CreateSemaphore RenderSemaphore with a maximum count equal to the number of rendering threads and initial count equal to 0.

In each rendering thread, do:
while(...)
{
WaitForSingleObject(RenderSemaphore, INFINITE);//Acquire semaphore to track # running threads
//Render Here//
ReleaseSemaphore(RenderSemaphore, 1, NULL)//Tell produce this thread finished
WaitForSingleObject(RenderEvent, INFINITE);//Wait for loop trigger
}
In the producer thread, do:
while(...)
{
//Create Data For Render Threads//
ResetEvent(RenderEvent);//Turn off loop trigger so render threads wait at end
ReleaseSemaphore(RenderSemaphore, NumRenderThreads, NULL);//Allow render threads to reacquire semaphore
for(size_t WaitCount = 0; WaitCount < NumRenderThreads; ++WaitCount)
{
WaitForSingleObject(RenderSemaphore, INFINITE);//Wait for all threads to finish
}
SetEvent(RenderEvent);//Tell render threads to loop
}
Might have a mistake or two, but I think that should give the gist of my idea

Share this post


Link to post
Share on other sites
Sounds like you want something more complext than a mutex. You want a locking mechanism that allows multiple simultaneous readers but exclusive writers, and you want a condition variable on which the readers will all wait.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!