信号唤醒线程

发布于 2024-09-06 01:06:50 字数 215 浏览 4 评论 0原文

我有一个正在将实时动画绘制到 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 技术交流群。

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

发布评论

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

评论(2

撩发小公举 2024-09-13 01:06:50

我不是渲染和开罗方面的专家,但线程通常可以互相发送信号。更简单的方法是使用条件变量或互斥体。所有工作线程都可以等待互斥体,并且 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.

弥繁 2024-09-13 01:06:50

您可以使用master-worker方法,其中主线程是处理要渲染的帧的master,并且workers从工作线程池。

DevGuy 您可以使用。

我认为这样的东西应该有效:

#include <dg/dg.h>
#include <dg/thread/threadpool.h>
#include <dg/thread/impl.h>
#include <dg/impl.h>
#include <boost/bind.hpp>

// a mutex is needed if the workers touch shared data
boost::mutex mutex;

struct work
{
    void operator ()()
    {
        boost::mutex::scoped_lock lock(mutex);

        // this is where a worker does its thing
    }

    worker(/* your constructor params */)
    {
        // ...
    }

    worker(const worker &w)
    {
        // ...
    }    

    // internal work data
};

int main(int argc, char* argv[])
{
    // a pool with 5 threads that ca queue up to 100 jobs
    dg::thread::ThreadPool pool(5,100);

    for (;;)
    {
        // create a piece of work
        work w;

        // execute the work
        pool.invoke(w);

        // some exit condition
    }

    // wait for unfinished work
    pool.wait();
}

这是另一种用法 示例

You could use a master-worker approach, where the main thread is the master that handles frames to be rendered and the workers are extracted from a pool 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:

#include <dg/dg.h>
#include <dg/thread/threadpool.h>
#include <dg/thread/impl.h>
#include <dg/impl.h>
#include <boost/bind.hpp>

// a mutex is needed if the workers touch shared data
boost::mutex mutex;

struct work
{
    void operator ()()
    {
        boost::mutex::scoped_lock lock(mutex);

        // this is where a worker does its thing
    }

    worker(/* your constructor params */)
    {
        // ...
    }

    worker(const worker &w)
    {
        // ...
    }    

    // internal work data
};

int main(int argc, char* argv[])
{
    // a pool with 5 threads that ca queue up to 100 jobs
    dg::thread::ThreadPool pool(5,100);

    for (;;)
    {
        // create a piece of work
        work w;

        // execute the work
        pool.invoke(w);

        // some exit condition
    }

    // wait for unfinished work
    pool.wait();
}

Here is another usage example.

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