Single threaded future/promises in D?

会有一股神秘感。 提交于 2019-12-07 07:08:52

问题


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

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.

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


回答1:


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




回答2:


The std.parallelism library can be used to simulate futures and promises. I have not tried it yet, but it is documented on the page for the library as well as an article discussing concurrency in D. It looks to be pretty easy even if it doesn't explicitly use the words "future" and "promise".



来源:https://stackoverflow.com/questions/21528393/single-threaded-future-promises-in-d

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!