receive

Receives a message from another thread.

Receive a message from another thread, or block if no messages of the specified types are available. This function works by pattern matching a message against a set of delegates and executing the first match found.

If a delegate that accepts a std.variant.Variant is included as the last argument to receive, it will match any message that was not matched by an earlier delegate. If more than one argument is sent, the Variant will contain a std.typecons.Tuple of all values sent.

void
receive
(
T...
)
(
T ops
)

Parameters

ops T

Variadic list of function pointers and delegates. Entries in this list must not occlude later entries.

Throws

OwnerTerminated when the sending thread was terminated.

Examples

import std.variant : Variant;

auto process = ()
{
    receive(
        (int i) { ownerTid.send(1); },
        (double f) { ownerTid.send(2); },
        (Variant v) { ownerTid.send(3); }
    );
};

{
    auto tid = spawn(process);
    send(tid, 42);
    assert(receiveOnly!int == 1);
}

{
    auto tid = spawn(process);
    send(tid, 3.14);
    assert(receiveOnly!int == 2);
}

{
    auto tid = spawn(process);
    send(tid, "something else");
    assert(receiveOnly!int == 3);
}

Meta