信号唤醒线程
我有一个正在将实时动画绘制到 gtkmm 窗口的程序。我有我的主线程 GUI 线程和一个工作线程,它使用 cairo 将动画帧渲染到图像表面。主线程每 33 毫秒向工作线程发出一次信号。现在,当必须渲染帧时,我的应用程序在每次超时时创建一个新的渲染线程。我该如何创建一种线程池,在 gui 线程中超时时,通知我的工作线程唤醒并渲染一个帧,该帧向 gui 线程发出该帧已完成的信号,然后返回睡眠状态并等待再次发出信号。
I have a programming that is drawing realtime animations to a gtkmm window. I have my main thread gui thread and a worker threa that renders a frame of the animation to a image surface using cairo. The worker thread has is signaled by the main thread every 33mS. Right now I have my app creating a new rendering thread on every timeout when a frame has to be rendered. How can I go about creating a kind of thread pool where on timeout in the gui thread signals my worker thread to wakeup and render a frame which signals the gui thread that frame is complete then go back to sleep and wait to be signaled again.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不是渲染和开罗方面的专家,但线程通常可以互相发送信号。更简单的方法是使用条件变量或互斥体。所有工作线程都可以等待互斥体,并且 GUI 线程可以在超时时释放互斥体,并且 GUI 线程本身可以去等待另一个线程。获取互斥体的工作线程可以渲染帧并向 GUI 线程发出信号,然后可以返回到第一个互斥体以等待来自 GUI 线程的下一个信号。
I am not an expert in rendering and cairo, but threads can in general signal each other. An easier way to do same is to use conditional variable or mutex. All your worker threads can wait on mutex and GUI thread can release mutex on time out and GUI thread itself can go and wait on another thread. Worker thread on acquiring mutex can render frame and signal GUI thread and then can go back to first mutex to wait for next signal from GUI thread.
您可以使用
master-worker
方法,其中主线程是处理要渲染的帧的master
,并且workers
从工作线程池。DevGuy 您可以使用。
我认为这样的东西应该有效:
这是另一种用法 示例。
You could use a
master-worker
approach, where the main thread is themaster
that handles frames to be rendered and theworkers
are extracted from apool
of worker threads.There is a public domain
thread pool
implementation available from DevGuy that you can use.I think something like this should work:
Here is another usage example.