Solved

Real time on Windows 7

Posted on 2013-01-24
11
574 Views
Last Modified: 2013-01-25
Hi Experts,
I had a wireframe object and had a file describing how it moved in time.  I was using C++ with Win32 libraries and the wireframe was running in an OpenGL window.  The file described a smooth motion through time without any quick movements, and the code I wrote to do the movements worked well- most of the time.  It worked very simply- with a Sleep(milliseconds) between each movement.  Again, the transition was bang on most of the time.  But sometimes the Sleep calls appeared to not wait at all and the mesh moved in an explosive display of fast movements as if there were no wait.  So always the same input, the same code, but different iterations of the same program with different results.  So is the problem the Windows clock?  (are there known issues for things like this).  And if yes, would a timer with a callback been better for some reason?  Or is what I'm trying to do here not very easily achieved?

Thanks for any help!
Mike
0
Comment
Question by:thready
  • 6
  • 3
  • 2
11 Comments
 
LVL 1

Author Comment

by:thready
ID: 38817255
So basically, the issue is, I have a series of discrete movements that need some time between them before changing location....
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 38817356
Hi thready,
The sleep approach is not a common practice, considering that it won't ensure you an specific period, because the render time is added to that timespan.
On the other hand, the timer approach is used regularly for ensuring always the same timespan between each render.
An intermediate approach without a timer is to use a time variable to wait for the proper time to render inside a inner while loop. In pseudocode it would be something like:

p = period in miliseconds
t0 = current_time
do
{
      t1 = t0 + p  
      Render_your_scene()
      while (current_time < t1)
      {
           sleep(10)    // sleep a very small amount of time in milliseconds
      }
      t0 = t1
} while (!some_exit_condition)
0
 
LVL 1

Author Comment

by:thready
ID: 38817366
Hi jaime- your code is the code that I am describing.  The sleep doesn't work (for the small amount of milliseconds), because I'm working with a medical device that runs at 60Hz- so the sleeps are often only about 4 milliseconds!  I'm wondering if the timer approach will work better because maybe it doesn't come from the same system clock as the sleep function uses?
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 38817381
If you basically need a better timer resolution, multimedia timers might be what yo're looking for, see http://www.codeproject.com/Articles/1236/Timers-Tutorial
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 38817385
in the code I have provided, it is not much meaningful the period of the sleep as far at it is slow, but the value of 'p'
the loop containing the sleep will be executed many times until the period 'p' is completed
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Comment

by:thready
ID: 38817390
ok so we could take p out of there basically- it's just a way to remove CPU starvation... Cool.
0
 
LVL 1

Author Comment

by:thready
ID: 38817392
sorry- i meant take the sleep out of there.......  :-P
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 38817401
if you remove that sleep, the CPU usage will raise to 100%
but I guess that is not a big deal in multi-core computers nowadays :)
0
 
LVL 1

Author Comment

by:thready
ID: 38817494
I noticed from jkr's link that on Windows, you start getting into trouble with timers below 10ms.  I wonder how precise multimedia timers can get.
0
 
LVL 86

Expert Comment

by:jkr
ID: 38819115
See yourself - http://msdn.microsoft.com/en-us/library/dd742877%28VS.85%29.aspx ("About Multimedia Timers"):

These timer services are useful for applications that demand high-resolution timing. For example, a MIDI sequencer requires a high-resolution timer because it must maintain the pace of MIDI events within a resolution of 1 millisecond.
0
 
LVL 1

Author Closing Comment

by:thready
ID: 38819369
This was just what I was looking for!  Thank you!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Windows Process running 3 48
I could not build boost code, 10 78
Create a path if not exists 7 69
Using popen() and gunzip() to open file in HTTPServer 6 49
What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now