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:
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.
Go is different, and a goroutine is not the same as a thread. Threads are much more expensive to create, use more memory and switching between threads takes longer. Goroutines are an abstraction over threads and a single Operating System thread can run many goroutines.
A goroutine is a function that executes simultaneously with other goroutines in a program and are lightweight threads managed by Go. A goroutine takes about 2kB of stack space to initialize.
An average process should have no problems with 100.000 concurrent routines.
Goroutines have easy communication medium known as channel. Thread does not have easy communication medium. Due to the presence of channel one goroutine can communicate with other goroutine with low latency. Due to lack of easy communication medium inter-threads communicate takes place with high latency.
There's nothing exactly equivalent but there are two modules that may provide something similar enough for your needs:
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".
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With