块和libdispatch在linux上可用吗?
我很想尝试GrandCentralDispatch,但我需要开发的只是一个Ubuntu工作站。是libdispatch,并且块扩展到c/obj-c等。。。linux上可用吗?如果是这样,我该如何获取它们呢?此时您可能需要使用LLVM编译器来获取块(我认为这在gcc中还不可用,但我还没有跟上gcc的步伐,所以我可能错了。) 目前正在努力移植到Linux。到目前为止,大部分工作似乎都在Debian上,但也有一些工作在其他发行版上。请参阅以下讨论主题:块和libdispatch在linux上可用吗?,linux,libdispatch,Linux,Libdispatch,我很想尝试GrandCentralDispatch,但我需要开发的只是一个Ubuntu工作站。是libdispatch,并且块扩展到c/obj-c等。。。linux上可用吗?如果是这样,我该如何获取它们呢?此时您可能需要使用LLVM编译器来获取块(我认为这在gcc中还不可用,但我还没有跟上gcc的步伐,所以我可能错了。) 目前正在努力移植到Linux。到目前为止,大部分工作似乎都在Debian上,但也有一些工作在其他发行版上。请参阅以下讨论主题: (已存档链接) 我做了一些工作,让OS X
- (已存档链接)
/// 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);
}
或者
使用clang-3.4
- sudo apt get安装libdispatch dev
- sudo apt get安装libblocks运行时开发
- 使用-fblock编译
- 链接到-lBlocksRuntime-ldispatch
/// 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(); });