linux上有块和libdispatch吗?

时间:2022-03-15 02:02:24

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?

我很想试试grand central dispatch,但我需要开发的是Ubuntu工作站。是libdispatch, block扩展到c/ object -c等…在linux上有空吗?如果是的话,我怎么得到它们?

4 个解决方案

#1


13  

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.)

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

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:

将libdispatch(开源libdispatch的老家)移植到Linux的工作正在进行中。到目前为止,大部分工作似乎都在Debian上,但也有一些在其他发行版上。看到这些讨论线程:

#2


6  

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/.

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

#3


0  

Use clang-3.4.

使用铿锵声- 3.4。

  • sudo apt-get install libdispatch-dev
  • sudo apt-get安装libdispatch-dev
  • sudo apt-get install libblocks-runtime-dev
  • sudo apt-get安装libblocks-runtime-dev
  • Compile with -fblocks
  • 编译与-fblocks
  • Link with -lBlocksRuntime -ldispatch
  • 与-lBlocksRuntime -ldispatch

#4


-2  

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

使用c++ lambdas而不是使用块。他们用c++玩得更好,而且隐藏的魔法也少了。

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(); });

#1


13  

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.)

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

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:

将libdispatch(开源libdispatch的老家)移植到Linux的工作正在进行中。到目前为止,大部分工作似乎都在Debian上,但也有一些在其他发行版上。看到这些讨论线程:

#2


6  

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/.

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

#3


0  

Use clang-3.4.

使用铿锵声- 3.4。

  • sudo apt-get install libdispatch-dev
  • sudo apt-get安装libdispatch-dev
  • sudo apt-get install libblocks-runtime-dev
  • sudo apt-get安装libblocks-runtime-dev
  • Compile with -fblocks
  • 编译与-fblocks
  • Link with -lBlocksRuntime -ldispatch
  • 与-lBlocksRuntime -ldispatch

#4


-2  

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

使用c++ lambdas而不是使用块。他们用c++玩得更好,而且隐藏的魔法也少了。

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(); });