Priority Messages and Thread Names

You will be familiarized with how messages can be sent with high priority and how names can be assigned to the threads in this lesson.

We'll cover the following

Priority messages

Messages can be sent with higher priority than regular messages by prioritySend(). These messages are handled before the other messages that are already in the mailbox:

prioritySend(ownerTid, ImportantMessage(100));

If the receiver does not have a message handler that matches the type of the priority message, then a PriorityMessageException is thrown:

std.concurrency.PriorityMessageException@std/concurrency.d(280): Priority message

Thread names

In the simple programs that we have used previously, it was easy to pass the thread ids of owners and workers. Passing thread ids from thread to thread may be overly complicated in programs that use more than a couple of threads. To reduce this complexity, it is possible to assign names to threads, which are globally accessible from any thread.

The following three functions define an interface of an associative array that every thread has access to:

  • register(): Associates a thread with a name

  • locate(): Returns the thread that is associated with the specified name; if there is no thread associated with that name, then Tid.init is returned

  • unregister(): Breaks the association between the specified name and the thread

The following program starts two threads that find each other using their names. These threads continuously send messages to each other until instructed to terminate by an Exit message:

Get hands-on with 1200+ tech skills courses.