Thread

This class encapsulates all threading functionality for the D programming language. As thread manipulation is a required facility for garbage collection, all user threads should derive from this class, and instances of this class should never be explicitly deleted. A new thread may be created using either derivation or composition, as in the following example.

class Thread {
version(NetBSD)
int fakePriority;
version(FreeBSD) shared
bool m_suspendagain;
version(Windows)
HANDLE m_hndl;
version(Darwin)
mach_port_t m_tmach;
version(Posix) shared
bool m_isRunning;
version(Solaris) __gshared
bool m_isRTClass;
package
Context m_main;
package
Context* m_curr;
package
bool m_lock;
package
void* m_tlsgcdata;
version(X86)
uint[8] m_reg;
version(X86_64)
ulong[16] m_reg;
version(X86)
uint[8] m_reg;
version(X86_64)
ulong[16] m_reg;
package __gshared
Context* sm_cbeg;
package __gshared
Thread sm_tbeg;
package __gshared
size_t sm_tlen;
package __gshared
Thread* pAboutToStart;
package __gshared
size_t nAboutToStart;
package
Thread prev;
package
Thread next;
}

Constructors

this
this(void function() fn, size_t sz = 0)

Initializes a thread object which is associated with a static D function.

this
this(void delegate() dg, size_t sz = 0)

Initializes a thread object which is associated with a dynamic D function.

Destructor

~this
~this()

Cleans up any remaining resources used by this object.

Members

Functions

id
ThreadID id()

Gets the OS identifier for this thread.

isDaemon
bool isDaemon()

Gets the daemon status for this thread. While the runtime will wait for all normal threads to complete before tearing down the process, daemon threads are effectively ignored and thus will not prevent the process from terminating. In effect, daemon threads will be terminated automatically by the OS when the process exits.

isDaemon
void isDaemon(bool val)

Sets the daemon status for this thread. While the runtime will wait for all normal threads to complete before tearing down the process, daemon threads are effectively ignored and thus will not prevent the process from terminating. In effect, daemon threads will be terminated automatically by the OS when the process exits.

isMainThread
bool isMainThread()

Tests whether this thread is the main thread, i.e. the thread that initialized the runtime

isRunning
bool isRunning()

Tests whether this thread is running.

join
Throwable join(bool rethrow = true)

Waits for this thread to complete. If the thread terminated as the result of an unhandled exception, this exception will be rethrown.

name
string name()

Gets the user-readable label for this thread.

name
void name(string val)

Sets the user-readable label for this thread.

priority
int priority()

Gets the scheduling priority for the associated thread.

priority
void priority(int val)

Sets the scheduling priority for the associated thread.

pushContext
void pushContext(Context* c)

////////////////////////////////////////////////////////////////////////

start
Thread start()

Starts the thread and invokes the function or delegate passed upon construction.

Static functions

PRIORITY_DEFAULT
int PRIORITY_DEFAULT()

The default scheduling priority that is set for a thread. On systems where multiple scheduling policies are defined, this value represents the default priority for the scheduling policy of the process.

PRIORITY_MAX
const(int) PRIORITY_MAX()

The maximum scheduling priority that may be set for a thread. On systems where multiple scheduling policies are defined, this value represents the maximum valid priority for the scheduling policy of the process.

PRIORITY_MIN
int PRIORITY_MIN()

////////////////////////////////////////////////////////////////////////

PRIORITY_MIN
int PRIORITY_MIN()

The minimum scheduling priority that may be set for a thread. On systems where multiple scheduling policies are defined, this value represents the minimum valid priority for the scheduling policy of the process.

add
void add(Context* c)

////////////////////////////////////////////////////////////////////////

add
void add(Thread t, bool rmAboutToStart = true)

////////////////////////////////////////////////////////////////////////

getAll
Thread[] getAll()

Provides a list of all threads currently being tracked by the system. Note that threads in the returned array might no longer run (see Thread..isRunning).

getThis
Thread getThis()

Provides a reference to the calling thread.

opApply
int opApply(scope int delegate(ref Thread) dg)

Operates on all threads currently being tracked by the system. The result of deleting any Thread object is undefined. Note that threads passed to the callback might no longer run (see Thread..isRunning).

sleep
void sleep(Duration val)

Suspends the calling thread for at least the supplied period. This may result in multiple OS calls if period is greater than the maximum sleep duration supported by the operating system.

slock
Mutex slock()

////////////////////////////////////////////////////////////////////////

yield
void yield()

Forces a context switch to occur away from the calling thread.

Structs

Context
struct Context

Examples

1 class DerivedThread : Thread
2 {
3     this()
4     {
5         super(&run);
6     }
7 
8 private:
9     void run()
10     {
11         // Derived thread running.
12     }
13 }
14 
15 void threadFunc()
16 {
17     // Composed thread running.
18 }
19 
20 // create and start instances of each type
21 auto derived = new DerivedThread().start();
22 auto composed = new Thread(&threadFunc).start();
23 new Thread({
24     // Codes to run in the newly created thread.
25 }).start();
Suggestion Box / Bug Report