Select in D Programming Language
import std.stdio;
import core.thread;
import core.time;
void main()
{
// For our example we'll select across two channels.
auto c1 = new Channel!string;
auto c2 = new Channel!string;
// Each channel will receive a value after some amount
// of time, to simulate e.g. blocking RPC operations
// executing in concurrent threads.
new Thread({
Thread.sleep(1.seconds);
c1.send("one");
}).start();
new Thread({
Thread.sleep(2.seconds);
c2.send("two");
}).start();
// We'll use `select` to await both of these values
// simultaneously, printing each one as it arrives.
foreach (i; 0..2)
{
select(
c1.recv().writeln("received ", _),
c2.recv().writeln("received ", _)
);
}
}
// A simple Channel implementation for this example
class Channel(T)
{
private T[] buffer;
private shared bool closed;
void send(T value)
{
synchronized(this)
{
buffer ~= value;
}
}
T recv()
{
while (true)
{
synchronized(this)
{
if (buffer.length > 0)
{
T value = buffer[0];
buffer = buffer[1..$];
return value;
}
if (closed)
throw new Exception("Channel closed");
}
Thread.sleep(1.msecs);
}
}
}
This D code demonstrates the concept of selecting from multiple channels, similar to the original example. Here’s an explanation of the key differences and adaptations:
We use D’s
import
statements to include necessary modules.Instead of goroutines, we use D’s
Thread
class to create concurrent operations.D doesn’t have built-in channels, so we’ve implemented a simple
Channel
class to mimic the behavior.The
select
statement in D is used differently. We’ve used a customselect
function (not shown in this example) that would need to be implemented to mimic Go’sselect
behavior.We use D’s
foreach
loop instead of a C-stylefor
loop.D’s
writeln
function is used instead offmt.Println
.Time durations are specified using D’s
Duration
type (e.g.,1.seconds
).
To run this program, you would save it as a .d
file and use the D compiler (dmd, ldc, or gdc) to compile and run it. For example:
$ dmd -run select.d
received one
received two
Note that the total execution time would still be about 2 seconds since both the 1 and 2 second sleeps execute concurrently.
This example demonstrates how D can be used to write concurrent programs with similar patterns to those used in other languages, albeit with some differences in syntax and available language features.