glutPostRedisplay 在不同的线程中

发布于 2024-08-08 20:50:17 字数 186 浏览 7 评论 0原文

我有一个标准的过剩实施。显示函数重绘每个对象,但我需要不断更新每个对象的某些值。事实上,我能想到的唯一方法是生成一个线程来处理更新。但是,我无法从不同的线程使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╭ゆ眷念 2024-08-15 20:50:17

如果您需要某种定期更新,您可能需要设置 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 try glutTimerFunc 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, or nanosleep 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 purposes glutTimerFunc 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 capital S), which takes a time in milliseconds.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文