nn::os::Thread Class

Header file: nn/os.h

Syntax

class Thread : public nn::os::WaitObject

Description

Class for representing threads.

Bug:The current implementation does not allow you to set the preferred processor or affinity mask.

Threads have priorities. The thread priority is specified as an integer from 0 to 31, with 0 indicating the highest priority. Standard threads specify the DEFAULT_THREAD_PRIORITY of 16.

Threads require a stack region to run. The management of the stack region can either be handled by the programmer, or you can leave it to be handled by the library.

When managing the stack region yourself, pass the stack region to the Start function and start the thread. Pass a stack region that is an instance of a class that has a function similar to uptr GetStackBottom(). The StackMemoryBlock and StackBuffer classes meet these requirements, so they can pass the stack region directly.
Note: When managing the stack region yourself, note that the stack region is never disabled (freed or otherwise invalidated) before the thread exits.

When letting the library manage the stack region, pass the stack size to the StartUsingAutoStack function and start the thread. The library allocates the stack region in 4096-byte chunks.

Thread objects become valid once Start-series functions have been left. Note that it is not at the point when a thread starts running. The IsValid function can be used to check whether an object is valid.

You must always call Join explicitly before destroying Thread objects that were started using the Start function. You cannot call Detach.

You must always call either Join or Detach explicitly before destroying Thread objects that were started using the StartUsingAutoStack function.

Thread objects inherit WaitObject objects; when a thread is released from waiting, it means that the thread has exited. When it's necessary to perform non-blocking Join operations, call a function like WaitOne or WaitObject::WaitAny first to verify that the thread has been released from waiting.

Note: In the current implementation, preferred processors and affinity masks can only be specified when starting a thread.

You can specify a preferred processor and affinity mask for threads. The preferred processor is specified with a value ranging from 0 to c-1, where c represents the number of cores. Alternately, specifying CORE_NO_ALL gives all processors equal priority as the preferred processor and uses an affinity mask that includes all processors. Specifying CORE_NO_USE_PROCESS_VALUE uses the default value of the associated application. Specifying a preferred processor will make the thread give priority to running on the specified processor, but it doesn't guarantee that the thread will run on the processor you specify.

Affinity masks specify which possible processors a thread prefers to run on. An affinity mask is expressed as an bit8 array. In each element, the least-significant bit indicates the first processor. Setting a particular bit to 1 gives permission for the thread to run on the corresponding processor. It's necessary to specify at least one processor; specifying masks with every bit set to zero is not allowed.

Using a processor number to specify the preferred processor when a thread is started creates an affinity mask in which the only bit that is set is that of the specified processor.

Member Functions

Thread Constructor.
~Thread Destructor.
Start Initializes and runs a thread.
TryStart Tries to initialize and run a thread.
StartUsingAutoStack Initializes and runs a thread.
TryStartUsingAutoStack Tries to initialize and run a thread.
Finalize Destroys a thread object.
Join Waits for a thread to exit.
Detach Detaches a thread.
IsAlive Gets whether a thread is alive.
GetId Gets the thread ID of an instance.
GetPriority Gets the thread priority of an instance.
ChangePriority Sets the thread priority of an instance.
GetAffinityMask Gets the affinity mask of an instance.
ChangeAffinityMask Sets the affinity mask of an instance.
GetIdealProcessor Gets the preferred processor number of an instance.
ChangeIdealProcessor Sets the preferred processor number of an instance.
S Sleep Puts the current thread to sleep for the time specified.
S Yield Gives threads with the same priority level as the current thread a chance to run.
S GetCurrentId Gets the thread ID of the current thread.
S GetCurrentPriority Gets the priority of the current thread.
S ChangeCurrentPriority Sets the priority of the current thread.
S GetCurrentAffinityMask Gets the affinity mask of the current thread.
S GetDefaultAffinityMask Gets the affinity mask of threads created using coreNo = CORE_NO_USE_PROCESS_VALUE.
S ChangeCurrentAffinityMask Sets the affinity mask of the current thread.
S SetDefaultAffinityMask Sets the affinity mask of threads created using coreNo = CORE_NO_USE_PROCESS_VALUE.
S GetCurrentIdealProcessor Gets the preferred processor number of the current thread.
S GetDefaultIdealProcessor Gets the preferred processor number of threads that are created using coreNo = CORE_NO_USE_PROCESS_VALUE.
S ChangeCurrentIdealProcessor Sets the preferred processor number of the current thread.
S SetDefaultIdealProcessor Sets the IdealProcessor value to use for threads that are created using coreNo = CORE_NO_USE_PROCESS_VALUE.
S GetCurrentProcessorNumber Gets the number of the processor that the current thread is running on.
S GetMainThread Gets the object that represents the main thread.

Class Hierarchy

ADLFireWall::NonCopyable
  nn::os::HandleObject
    nn::os::WaitObject
      nn::os::Thread

Revision History

2010/01/07
Initial version.

CONFIDENTIAL