在D语言中是否有类似goroutines的等效功能?

huangapple go评论81阅读模式
英文:

Is there an equivalent of goroutines in D?

问题

我喜欢Go,尤其是goroutines。它们简单高效。经过一些调查,它们似乎基本上是在一组内核线程上多路复用的纤程(如果我理解错了,请纠正我)。

话虽如此,D语言中是否有任何标准库(或相对流行且受支持的第三方扩展)?

我主要想要的是:

  • 轻量级- 线程使用太多内存并且占用太多CPU
  • 简单- 数据共享不是太重要,但简单的消息传递是
  • 受管理- 最好是在运行时级别上进行管理

这里的主要目标是尽可能高效地创建一个能与Node.js和Go的速度相媲美的Web服务器。这意味着可能会有许多活动连接(http、websockets、数据流)。

我喜欢其他提到的平台的一些特点,但D语言更加通用。如果不太笨重的话,我会选择D语言而不是其他语言。

英文:

I love Go, especially the goroutines. They are simple and efficient. After some digging, it seems that they are basically fibers multiplexed onto a pool of kernel threads (correct me if I'm wrong).

That being said, are there any standard libraries (or relatively popular and supported 3rd party additions) in D?

The main things I want are:

  • Lightweight- threads use too much memory and take too much CPU
  • Simple- data-sharing isn't too important, but simple message passing is
  • Managed- it would be nice for this to be at the run-time level

The main goal here is to make a web server as efficiently as possible to rival the speed of Node.js and Go. This means there could be many active connections (http, websockets, data streaming).

I like things about the other platforms mentioned, but D is much more generalized. If it isn't too clunky, I would choose D over the others.

答案1

得分: 4

没有完全等效的东西,但有两个模块可能提供足够相似的功能满足您的需求:

  1. std.concurrency 提供消息传递和保证隔离,除非使用shared限定符来获得非常有限的显式共享内存。然而,您目前无法像goroutines那样将纤程复用到线程上。现在,每次调用spawn都会启动一个新的操作系统线程。此外,还有一些工作要做,以使不变性足够可用,以使这种范式发挥其全部潜力。有关此范式的更多信息,请参阅Andrei Alexandrescu的《D编程语言》免费章节

  2. std.parallelism 提供任务。它面向并行性,而不是并发性。因此,任务不会进行消息传递,而是在没有与调用线程通信的情况下执行,然后将其返回值返回给调用线程。此外,如果任务数超过线程数,多余的任务将被排队,而不是使用纤程进行复用。

编辑:我最初设计并编写了std.parallelism,并愿意考虑满足您需求的增强请求,只要它们不会将模块的范围扩展得太远,涉及一般情况的并发性。如果std.parallelism几乎满足您的需求,但不完全满足,请在此处或digitalmars.d新闻组上发布功能请求。

此外,即使我可能不是这样一个请求的实现者,也可以随时提出对std.concurrency的改进建议。

英文:

There's nothing exactly equivalent but there are two modules that may provide something similar enough for your needs:

  1. std.concurrency provides message passing and guaranteed isolation unless the shared qualifier is used to gain very limited, explicit shared memory. However, you don't (yet) get the multiplexing of fibers onto threads that goroutines provide. Right now, every call to spawn starts a new OS thread. Also, there's still some work to be done to make immutability usable enough to make this paradigm reach its full potential. For more on this paradigm, see the free chapter of Andrei Alexandrescu's "The D Programming Language".

  2. std.parallelism provides tasks. It's geared towards parallelism, not concurrency. (These are not the same thing even though you need concurrency to implement parallelism.) Therefore, instead of message passing, a task simply executes with no communication with the calling thread and then returns its return value to the calling thread. Additionally, if there are more tasks than threads, the excess tasks are queued, not multiplexed using fibers.

Edit: I originally designed and wrote std.parallelism and am willing to consider enhancement requests to suit needs such as yours, as long as they don't expand the scope of the module too far into general-case concurrency. If std.parallelism does almost what you need but not quite, please post a feature request either here or on the digitalmars.d newsgroup.

Also, even though I would likely not be the implementer of such a request, feel free to suggest enhancements to std.concurrency.

答案2

得分: 2

std.parallel使用线程池来运行任务,但是您需要自己实现消息传递例程(目前库中没有可用的线程安全队列)。

英文:

std.parallel uses threadpools to run tasks however you'll need to implement your own message passing routines (there is currently no threadsafe queue available in the library AFAIK)

答案3

得分: 0

我不知道D语言的库是否能提供分割堆栈的支持(给线程/纤程)。如果没有这个功能,很遗憾地失去了很多Go语言的goroutines的有用性。

如果某个问题使用goroutines更容易/更好解决,为什么不一开始就使用Go语言呢?

英文:

I don't know if a D library can provide split stacks support (to threads/fibers). Without it a lot of the Go goroutines usefulness is unfortunately lost.

If some problem is easy/better to solve using goroutines then why not just use Go in the first place?

huangapple
  • 本文由 发表于 2011年12月7日 15:58:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/8412056.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定