Skip to content
Tutorials Center
My learning

Topics

Lesson 28 of 36 ยท Concurrency and Memory

Message Passing with std.concurrency in D

Learn message-passing concurrency in D: spawn, Tid, send, receive, receiveOnly, thread ownership and shutdown, with a C stand-in using a mutex-protected message queue.

Sharing memory between threads is difficult to get right. D's std.concurrency module offers another model: threads share nothing by default and communicate by sending messages. Each thread has a mailbox; one thread sends a message, and the receiver picks it up with receive. This is the same idea as actors in Erlang or channels in Go, and it avoids most locking.

D code is read-only on this site. The C stand-ins build a mailbox from a mutex and a condition variable using POSIX threads.

Spawning a thread and sending a message

D
import std.concurrency;
import std.stdio;

void worker()
{
    auto n = receiveOnly!int();          // wait for one int
    ownerTid.send(n * n);                // reply to the thread that spawned us
}

void main()
{
    Tid child = spawn(&worker);
    child.send(12);
    auto answer = receiveOnly!int();
    writeln("12 squared is ", answer);   // 144
}
  • spawn(&fn, args...) starts a new thread running fn and returns its Tid (thread id).
  • tid.send(values...) places a message in that thread's mailbox. It does not block.
  • receiveOnly!T() blocks until a message of exactly type T arrives. A message of another type throws MessageMismatch.
  • ownerTid is the Tid of the thread that spawned this one; thisTid is your own.

Messages are copied, so the two threads never touch the same memory. D enforces this: only data that cannot be shared mutably (values, immutable data, shared data) may be sent. Passing a pointer to a mutable local is rejected at compile time. That is the guarantee that makes this model safe.

receive with pattern matching

A worker usually handles several kinds of message. receive takes a list of delegates and runs the one whose parameter types match the incoming message:

D
void logger()
{
    bool running = true;
    while (running)
    {
        receive(
            (string line) { writeln("LOG: ", line); },
            (int code)    { writeln("code ", code); },
            (bool stop)   { running = false; },
            (Variant v)   { writeln("unknown message"); }
        );
    }
}

receive waits for any message that has a handler. It looks through the mailbox in order and takes the first matching message, so a message nobody handles stays queued, which can fill memory. Add a Variant catch-all, or use receiveTimeout(100.msecs, ...) to avoid waiting forever.

Shutdown protocols

Threads do not exit by themselves, so design the shutdown. Common choices:

  1. Send a stop message (as above) and join-style wait for a confirmation reply.
  2. Send OwnerTerminated handling: if the owner dies, workers receive an OwnerTerminated message; if you do not handle it, receive throws.
  3. Use a poison pill, a special value meaning "no more work".

A mailbox in C

The C version below has the same ingredients: a queue guarded by a mutex, and a condition variable so the receiver sleeps until something arrives. The producer sends numbers, then a stop value (a poison pill).

C Stand-in: a thread mailbox with send and receive

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Messages arrive in the order sent, because one sender puts them in one queue. With several senders the order between different senders is not fixed.

Request and reply

A common pattern sends the sender's own Tid inside the request so the worker knows where to answer:

D
void server()
{
    for (;;)
    {
        receive((Tid client, int x) { client.send(x * 2); });
    }
}

The client calls server.send(thisTid, 21); and then receiveOnly!int(). A second stand-in turns it into two mailboxes: one for requests, one for replies.

C Stand-in: request and reply through two mailboxes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

When to use which model

Need Model
Split one big loop over cores parallel foreach
Independent services or pipelines that talk message passing
A shared cache or counter with simple rules shared + synchronized or atomics
Thousands of lightweight tasks fibers (core.thread.Fiber, vibe.d)

Message passing costs a copy per message and some latency. For high volume, send batches or large immutable slices rather than single bytes.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does spawn return?

    A Tid is the address you pass to send.
  2. 2. What happens when receiveOnly!int() gets a string message?

    receiveOnly requires exactly the stated type.
  3. 3. Why can you not send a pointer to a mutable local variable?

    Only unshared-safe data (copies, immutable, shared) may cross threads.
  4. 4. What is a poison pill?

    It is a sentinel message that ends the worker's loop cleanly.

Summary

  • Threads communicate with copied messages: spawn, send, receive, receiveOnly.
  • receive pattern-matches handlers by parameter type; plan for unmatched messages.
  • Design shutdown explicitly (stop message, OwnerTerminated, poison pill).
  • The type system blocks sending mutable shared data, which removes most races.

Next: The Garbage Collector