Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an equivalent of goroutines in 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.

like image 676
beatgammit Avatar asked Dec 07 '11 07:12

beatgammit


People also ask

Are Goroutines just threads?

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.

What are 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.

What is the max number of Goroutines?

An average process should have no problems with 100.000 concurrent routines.

Why are Goroutines better than threads?

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.


2 Answers

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.

like image 133
dsimcha Avatar answered Sep 20 '22 04:09

dsimcha


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)

like image 43
ratchet freak Avatar answered Sep 21 '22 04:09

ratchet freak