过剩和多线程
如果我有一个主线程设置一个过剩窗口,然后创建一个工作线程来进行计算,并且在对工作线程中的某些事件进行计算期间我想更新我的过剩窗口。我怎样才能做到这一点? 显然我无法直接从工作线程调用 glut 函数。
我考虑设置一些在过剩空闲循环中检查的“更新标志”,如果设置了它,则重新绘制过剩窗口,但显然从空闲循环中调用过剩函数也不起作用(或者至少不应该)完成)。
那么 - 正确的方法是什么?
谢谢!
if I have a mainthread that sets up a GLUT window and then create a working thread to do calculations and during the calculations upon some events in the working thread i want to update my GLUT window. How can I do that?
Apparently I can't call the glut functions directly from the working thread.
I thoght of setting some 'updateflag' that gets checked in the glut Idle Loop and if it's set then to redraw the GLUT window, but aparently it doesn't work to call glut functions from the idle loop either (or at least shouldn't be done).
So - what's the proper way to do that?
thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,我已经在 GLUT 上实现了多线程来进行计算,我所做的是一个 bool 标志,一旦另一个线程上发生了事件,它就会告诉 glut,然后我有一个堆栈,其中包含在另一个线程上收集的数据,然后重用堆栈在主 GLUT 线程上,通过这种方式,我设法保持 glut 工作但不停止渲染。
我使用 Async <- 来实现多线程函数
well I've implemented multi-thread on GLUT for calculations what I do it's ad a bool flag that tells the glut once an event has happened on the other thread, then I have a stack with data collected on the other thread and just reuse the stack on the main GLUT thread, on that way I manage to keep glut working but not stopping the rendering.
I use Async <- for multithread functions
这是大多数 GUI 框架的常见问题(大多数通常不是线程安全的)。
正常的解决方案是将调用发布到主事件循环。我不确定您使用什么线程库(有些可能有委托等概念),但在您的情况下,一个简单的事件队列可能会做到这一点。
是的,您可以从空闲函数重绘窗口。至少我一直都是这么做的...
That's a common problem with most GUI frameworks (most are normally not threadsafe).
The normal solution is posting invocations to the main event loop. I am not sure what thread library you use (some will probably have the concept of delegates etc), but in your case a simple event queue would probably do it.
And yes, you can redraw the window from the idle function. At least I've always done it...