Note: This website is archived. For up-to-date information about D projects and development, please visit wiki.dlang.org.

Thread Library

This library holds functionality relating to MiniD's coroutines, as well as defining the thread type's metatable.

Like the modules library, this library is kind of part of the base library and will always be loaded.

Members

  1. thread.traceback()
  2. thread.halt([t: thread])
  3. thread.current()
  4. Thread metamethods
    1. reset([func: function])
    2. state()
    3. isInitial(), isRunning(), isWaiting(), isSuspended(), isDead()

thread.traceback()

Gets a string representing the call stack that was unwound during the last exception being thrown. You can use this to print out more meaningful errors if you catch an exception in your MiniD code, i.e.

try
{
	// ...
}
catch(e)
{
	writeln("caught exception: ", e)
	writeln(thread.traceback())
}

If there is no traceback information available, this simply returns an empty string.

thread.halt([t: thread])

Halts a given thread, causing it to enter the "dead" state but without causing an error. This is one of two ways a thread can successfully enter the "dead" state; the other is to return from its top-level function. If a parameter is given, it should be a thread to halt. If no parameter is given, it defaults to the current thread.

thread.current()

This returns the thread object of the currently-executing thread, or null if the current thread is the main thread.

Thread metamethods

Metamethods for thread objects. These functions are all accessed as methods of threads.

reset([func: function])

Once a coroutine is in the 'dead' state, it's pretty much useless. However, you can use this method to reset a dead coroutine, placing it back in the 'initial' state so you can start it over again. Throws an error if the coroutine isn't in the 'dead' state.

The optional func parameter can be used to reset a coroutine object but give it a new function to use. You can get almost the same result by using a coroutine expression, except using the reset method of a dead coroutine object allows you to save a memory allocation by reusing an otherwise useless object.

state()

Returns a string representing the current state of the thread. Possible return values are "initial", "running", "waiting", "suspended", and "dead".

isInitial(), isRunning(), isWaiting(), isSuspended(), isDead()

These all return boolean values of whether the thread is in that state or not. These are a bit shorter to type than comparing the result of state() to a string literal.