使用 gtkD 的多线程应用程序

发布于 2024-12-23 12:27:28 字数 895 浏览 1 评论 0原文

我在玩 gtkD 一段时间,同时也在学习 D2/Phobos。昨天我正在查找 std.concurrency 模块并尝试编写一个玩具多线程分形查看器,但问题是我看不到多线程与 gtkD 一起工作的方式。

现在,我有这个:

import std.concurrency;

class TestMainWindow : MainWindow
{
    this() {
        super("test");
        ...
        spawn(&worker);
    }

    public void notify() {
        m_progress.pulse();
    }

    private ProgressBar m_progress;
}

shared(TestMainWindow) window;

main(string[] args) {
    Main.init(args);
    window = new shared(TestMainWindow)();
    Main.run();
}

void worker() {
    for (int i = 0; i < 20; ++i) {
        (cast(TestMainWindow) window).notify();
        Thread.sleep(dur!"msecs"(200));
    }
}

在 Andrei 的书中,在并发章节中,有消息传递范例,我想 适用,但问题是 gtk 主循环对我来说是隐藏的。我不喜欢上面的代码,因为它 转换为非共享很难看并且可能不安全。 那么有没有什么方法可以继承“线程无关”的类,使其具有线程感知能力,以及什么是 gtkD 中用于编程多线程应用程序的标准机制?我见过 gthread.Thread 模块, 但它的作用似乎只是作为外部 C gtk+ 线程功能的接口。

I'm playing with gtkD for a while, and I'm learning D2/Phobos in parallel. Yesterday I was looking up the std.concurrency module and tried to write a toy multithreaded fractal viewer, but the problem is that I can't see the way multithreading works with gtkD.

Now, I have this:

import std.concurrency;

class TestMainWindow : MainWindow
{
    this() {
        super("test");
        ...
        spawn(&worker);
    }

    public void notify() {
        m_progress.pulse();
    }

    private ProgressBar m_progress;
}

shared(TestMainWindow) window;

main(string[] args) {
    Main.init(args);
    window = new shared(TestMainWindow)();
    Main.run();
}

void worker() {
    for (int i = 0; i < 20; ++i) {
        (cast(TestMainWindow) window).notify();
        Thread.sleep(dur!"msecs"(200));
    }
}

In the Andrei's book, in the chapter for concurrency, there's the message passing paradigm, which I want to
apply, but the problem is that the gtk main loop is hidden from me. I don't like the above code, because its
ugly to cast to non-shared and likely unsafe.
So is there some way to inherit a "thread-agnostic" class, making it thread-aware, and what is the
standard mechanism in gtkD to program multithreaded applications? I've seen the gthread.Thread module,
but its role seems to be only as an interface to the external C gtk+ threading capabilities.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

木槿暧夏七纪年 2024-12-30 12:27:28

不幸的是,我很确定答案是否定的。 GtkD 是在共享存在之前设计的,并且支持 D1 和 D2。此外,共享的问题太多,目前还无法使用。因此,GtkD 不支持共享,并且可能暂时不会支持。

Unfortunately I'm pretty sure the answer is no. GtkD was designed before shared existed and supports both D1 and D2. Furthermore, shared is so buggy it's not usable yet. Therefore, GtkD doesn't support shared and probably won't for a while.

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