块和 libdispatch 在 Linux 上可用吗?

发布于 2024-10-31 20:33:53 字数 114 浏览 1 评论 0原文

我很想尝试 Grand Central Dispatch,但我必须在 Ubuntu 工作站上进行开发。 libdispatch 以及 c/obj-c 等的块扩展在 Linux 上可用吗?如果是这样,我如何获得它们?

I would love to try out grand central dispatch, but all I have to develop on is an Ubuntu workstation. Is libdispatch, and the blocks extension to c/obj-c etc... available on linux? If so, how do I get them?

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

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

发布评论

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

评论(4

甚是思念 2024-11-07 20:33:53

您可能需要使用 LLVM Clang(在 Ubuntu 上可用) 编译器此时获取块(我不认为这在 gcc 中可用,但我还没有跟上 gcc,所以我可能是错的。)

正在努力移植 libdispatch(开源 libdispatch 的主页) 到 Linux。到目前为止,大部分工作似乎都在 Debian 上,但也有一些在其他发行版上。请参阅这些讨论主题:

You may need to use the LLVM Clang (available on Ubuntu) compiler to get blocks at this time (I don't think this is available in gcc yet, but I haven't been keeping up with gcc, so I could be wrong.)

There are efforts underway to port libdispatch (home for the open source libdispatch) to Linux. Most of the effort seems to be on Debian so far, but some on other distributions, too. See these discussion threads:

緦唸λ蓇 2024-11-07 20:33:53

我已经做了一些工作,让 OS X Mountain Lion 版本的 libdispatch 在 Linux 上运行;结果在 Github 上:http://nickhutchinson.me/libdispatch/

I've done some work to get the OS X Mountain Lion version of libdispatch working on Linux; the result is up at Github: http://nickhutchinson.me/libdispatch/.

月亮邮递员 2024-11-07 20:33:53

使用 clang-3.4。

  • sudo apt-get install libdispatch-dev
  • sudo apt-get install libblocks-runtime-dev
  • 使用 -fblocks 编译
  • 使用 -lBlocksRuntime -ldispatch 链接

Use clang-3.4.

  • sudo apt-get install libdispatch-dev
  • sudo apt-get install libblocks-runtime-dev
  • Compile with -fblocks
  • Link with -lBlocksRuntime -ldispatch
ゃ懵逼小萝莉 2024-11-07 20:33:53

使用 C++ lambda,而不是使用块。他们使用 C++ 玩得更好,并且隐藏的魔法更少。

我这样做:

/// Dispatch a function object to a queue.
template<class F>
static void dispatch_async_function(dispatch_queue_t queue, F f) {
    struct context_t {
        using function_type = F;

        context_t(function_type&& f) noexcept
        : _f(std::move(f))
        {}

        static void execute(void* p) noexcept {
            auto context = reinterpret_cast<context_t*>(p);
            if (context) {
                try {
                    context->_f();
                }
                catch(...) {
                    // error processing here
                }
                delete context;
            }
        }

    private:
        function_type _f;
    };

    dispatch_async_f(queue, new context_t<F>(std::move(f)), &context_t<F>::execute);
}

如果您需要确保在调用发生之前存在某些共享资源(例如通过共享指针保持活动状态的对象上的回调):

/// Dispatch a function object to a queue. Only execute the function if the tie
/// locks successfully.
template<class F>
static void dispatch_async_tied_function(dispatch_queue_t queue, std::weak_ptr<void> tie, F f) {
    struct context_t {
        using function_type = F;

        context_t(function_type&& f) noexcept
        : _f(std::move(f))
        {}

        static void execute(void* p) noexcept {
            auto context = reinterpret_cast<context_t*>(p);
            auto lock = _tie.lock();
            if (context && tie) {
                try {
                    context->_f();
                }
                catch(...) {
                    // error processing here
                }
                delete context;
            }
        }

    private:
        function_type _f;
        std::weak_ptr<void> _tie;
    };

    dispatch_async_f(queue, new context_t<F>(std::move(f)), &context_t<F>::execute);
}

像这样调用它们

dispatch_function(queue, []() { something(); });

或...

dispatch_tied_function(_myQueue, shared_from_this(), [this]() { somethingOnThis(); });

Rather than use blocks, use c++ lambdas. They play better with c++ and there is less hidden magic.

I do it like this:

/// Dispatch a function object to a queue.
template<class F>
static void dispatch_async_function(dispatch_queue_t queue, F f) {
    struct context_t {
        using function_type = F;

        context_t(function_type&& f) noexcept
        : _f(std::move(f))
        {}

        static void execute(void* p) noexcept {
            auto context = reinterpret_cast<context_t*>(p);
            if (context) {
                try {
                    context->_f();
                }
                catch(...) {
                    // error processing here
                }
                delete context;
            }
        }

    private:
        function_type _f;
    };

    dispatch_async_f(queue, new context_t<F>(std::move(f)), &context_t<F>::execute);
}

And if you need to ensure that some shared resource exists before the call takes place (such as a callback on an object that is kept alive by a shared pointer):

/// Dispatch a function object to a queue. Only execute the function if the tie
/// locks successfully.
template<class F>
static void dispatch_async_tied_function(dispatch_queue_t queue, std::weak_ptr<void> tie, F f) {
    struct context_t {
        using function_type = F;

        context_t(function_type&& f) noexcept
        : _f(std::move(f))
        {}

        static void execute(void* p) noexcept {
            auto context = reinterpret_cast<context_t*>(p);
            auto lock = _tie.lock();
            if (context && tie) {
                try {
                    context->_f();
                }
                catch(...) {
                    // error processing here
                }
                delete context;
            }
        }

    private:
        function_type _f;
        std::weak_ptr<void> _tie;
    };

    dispatch_async_f(queue, new context_t<F>(std::move(f)), &context_t<F>::execute);
}

call them like this

dispatch_function(queue, []() { something(); });

or...

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