D中的单线程将来/承诺? [英] Single threaded future/promises in D?

查看:69
本文介绍了D中的单线程将来/承诺?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到D有期货,并且可以创建线程,但是是否有类似 Dart的期货/承诺(或者,如果您使用的是库,我猜是JavaScript)吗?

I see that D has futures, and can create threads but is there anything like "Dart" futures/promises (or I guess javascript if you use a library)?

我希望能够编写这样的代码-

I want to be able to write code like this -

//
// NOT D code because I can't remember all the syntax!
//
auto fut = myfile.read();

fut.then(function(data) { 
    // Process data asynchronously when the future completes
};

我希望在与主代码相同的线程上进行回调。这大概需要某种均匀的队列和事件分派。
我认为ASIO在boost中提供了一些类似的C ++功能,但是语法更好的东西会很好。

I want the callback on the same thread as the main code though. This will presumably need some kind of even queue and event dispatching. I think that ASIO in boost provides some similar facilities for C++ but something with a better syntax would be nice.

核心D中是否有类似的东西,或者有任何简单的方法来实现单线程

Is there anything like this in core D, or any simple way to achieve single threaded futures for async code?

推荐答案

我建议您看一下vibe.d: http://vibed.org/
它使您可以编写高性能的事件驱动代码,就像从nodejs所知道的那样,但是可以使用D的光纤以同步的方式进行: http://dlang.org/phobos/core_thread.html#.Fiber

I suggest you take a look at vibe.d: http://vibed.org/ It enables you to write performant event driven code like you know from nodejs but in a synchronous manner using D's fibers: http://dlang.org/phobos/core_thread.html#.Fiber

这篇关于D中的单线程将来/承诺?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆