glutPostRedisplay 在不同的线程中
我有一个标准的过剩实施。显示函数重绘每个对象,但我需要不断更新每个对象的某些值。事实上,我能想到的唯一方法是生成一个线程来处理更新。但是,我无法从不同的线程使用 glutPostRedisplay() 来让 glut 刷新窗口。在 glut 循环旁边有一个循环来更新值的好方法是什么?
另外,睡眠几分之一秒的最佳方法是什么(而不是睡眠整秒)。
I have a standard glut implementation. The display function redraws each object, but I need a constant update on certain values of each object. As it is, the only way I can think to do this is to spawn a thread to handle the updating. However, I can't use glutPostRedisplay() from a different thread to get glut to refresh the window. What is a good way to have a loop to update values alongside the glut loop?
Also, what's the best way to sleep for fractions of seconds (instead of sleep() for whole seconds).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您需要某种定期更新,您可能需要设置
glutIdleFunc
。这是一个每当没有事件需要处理时就会循环调用的函数。如果您想定期调用某些内容(而不是尽可能快地调用),您可能需要尝试glutTimerFunc
允许您安排 GLUT 循环在未来的几毫秒内运行某些内容。至于你的第二个问题,如果你需要睡眠几分之一秒,你可能想尝试
usleep
用于微秒分辨率的睡眠周期,或nanosleep
以纳秒为单位指定睡眠周期(尽管您实际上不会获得纳秒分辨率)。我不知道您使用的是什么平台,也不知道这些是否可以在 Windows 上使用,但它们应该可以在任何 POSIX 兼容系统(Linux、BSD、Mac OS X)上使用。但也许出于您的目的,glutTimerFunc
会工作得更好。编辑添加:看起来在 Windows 上您需要使用 [
Sleep
](http://msdn.microsoft.com/en-us/library/ms686298(VS.85%29.aspx)(注意大写的S
),这需要以毫秒为单位的时间。If you need some sort of regular updating, you probably want to set a
glutIdleFunc
. This is a function that will get called in a loop whenever there are no events coming in to be processed. If you instead want to call something at regular intervals (as opposed to as fast as possible), you might want to tryglutTimerFunc
which allows you to schedule something to be run by the GLUT loop some number of milliseconds in the future.As for your second question, if you need to sleep for fractions of seconds, you might want to try
usleep
for microsecond resolution sleep periods, ornanosleep
to specify sleep periods in nanoseconds (though you're not actually going to get nanosecond resolution). I don't know what platform you're on or if these are available on Windows, but they should be available on any POSIX compatible system (Linux, BSD, Mac OS X). But perhaps for your purposesglutTimerFunc
will work better.edit to add: It looks like on Windows you would need to use [
Sleep
](http://msdn.microsoft.com/en-us/library/ms686298(VS.85%29.aspx) (note the capitalS
), which takes a time in milliseconds.