Thread class
Owns a Win32 thread handle and closes it on destruction.
- Use
import ThinkMeta.ConcurrentTasks.Windows.Thread;- Namespace
ThinkMeta::ConcurrentTasks::Windows
Syntax
class ThreadRemarks
Owning the handle does not control the thread: destroying the object leaves the thread running. Members report failures as ErrorCode. Members that act on the thread require a handle (asserted in debug builds). Use one object from one thread at a time; the thread APIs it wraps are thread-safe.
See also
Constructors
Thread
~Thread
Methods
operator=
IsValid
Returns whether the object holds a handle.
Syntax
bool IsValid() const noexcept;Return value
Type: bool
true if a handle is held; otherwise false.
Open
Opens an existing thread by id (OpenThread).
Syntax
static ErrorCode Open(
DWORD desiredAccess,
BOOL inheritHandle,
DWORD threadId,
Thread& thread
) noexcept;Parameters
desiredAccessDWORD- Access rights the handle gets, e.g.
THREAD_QUERY_LIMITED_INFORMATIONorTHREAD_ALL_ACCESS. inheritHandleBOOLTRUEto let child processes inherit the handle.threadIdDWORD- Id of the thread to open.
threadThread&- Receives the handle; a handle it held before is closed.
Return value
Type: ErrorCode
Error::Success (0) on success; otherwise the Win32 error code.
See also
Create
Creates a thread in this process (CreateThread) and keeps its handle.
Syntax
ErrorCode Create(
SECURITY_ATTRIBUTES* threadAttributes,
SIZE_T stackSize,
PTHREAD_START_ROUTINE startAddress,
void* parameter,
DWORD creationFlags,
DWORD* threadId
) noexcept;Parameters
threadAttributesSECURITY_ATTRIBUTES*- Security descriptor and inheritance of the handle;
nullptr: default security, not inheritable. stackSizeSIZE_T- Initial stack size in bytes;
0: the executable's default. startAddressPTHREAD_START_ROUTINE- The thread function; required.
parametervoid*- Passed unchanged to
startAddress; may benullptr. creationFlagsDWORD0to run at once,CREATE_SUSPENDEDto start suspended (seeResume),STACK_SIZE_PARAM_IS_A_RESERVATIONto reserve rather than commitstackSize.threadIdDWORD*- Receives the new thread's id; may be
nullptr.
Return value
Type: ErrorCode
Error::Success (0) on success; otherwise the Win32 error code.
Remarks
The object must not hold a handle yet. The new thread runs startAddress as a plain OS thread, not as a task.
See also
CreateRemote
Creates a thread in another process (CreateRemoteThread).
Syntax
ErrorCode CreateRemote(
HANDLE process,
SECURITY_ATTRIBUTES* threadAttributes,
SIZE_T stackSize,
PTHREAD_START_ROUTINE startAddress,
void* parameter,
DWORD creationFlags,
DWORD* threadId
) noexcept;Parameters
processHANDLE- Handle of the target process with
PROCESS_CREATE_THREAD,PROCESS_QUERY_INFORMATION,PROCESS_VM_OPERATION,PROCESS_VM_WRITEandPROCESS_VM_READaccess; borrowed. threadAttributesSECURITY_ATTRIBUTES*- Security descriptor and inheritance of the handle;
nullptr: default security, not inheritable. stackSizeSIZE_T- Initial stack size in bytes;
0: the executable's default. startAddressPTHREAD_START_ROUTINE- The thread function, an address in the target process; required.
parametervoid*- Passed unchanged to
startAddress; may benullptr. creationFlagsDWORD0to run at once,CREATE_SUSPENDEDto start suspended,STACK_SIZE_PARAM_IS_A_RESERVATIONto reserve rather than commitstackSize.threadIdDWORD*- Receives the new thread's id; may be
nullptr.
Return value
Type: ErrorCode
Error::Success (0) on success; otherwise the Win32 error code.
Remarks
The object must not hold a handle yet.
See also
Creates a thread in another process (CreateRemoteThreadEx).
Syntax
ErrorCode CreateRemote(
HANDLE process,
SECURITY_ATTRIBUTES* threadAttributes,
SIZE_T stackSize,
PTHREAD_START_ROUTINE startAddress,
void* parameter,
DWORD creationFlags,
LPPROC_THREAD_ATTRIBUTE_LIST attributeList,
DWORD* threadId
) noexcept;Parameters
processHANDLE- Handle of the target process with
PROCESS_CREATE_THREAD,PROCESS_QUERY_INFORMATION,PROCESS_VM_OPERATION,PROCESS_VM_WRITEandPROCESS_VM_READaccess; borrowed. threadAttributesSECURITY_ATTRIBUTES*- Security descriptor and inheritance of the handle;
nullptr: default security, not inheritable. stackSizeSIZE_T- Initial stack size in bytes;
0: the executable's default. startAddressPTHREAD_START_ROUTINE- The thread function, an address in the target process; required.
parametervoid*- Passed unchanged to
startAddress; may benullptr. creationFlagsDWORD0to run at once,CREATE_SUSPENDEDto start suspended,STACK_SIZE_PARAM_IS_A_RESERVATIONto reserve rather than commitstackSize.attributeListLPPROC_THREAD_ATTRIBUTE_LIST- Extended attributes of the new thread (
InitializeProcThreadAttributeList); may benullptr. threadIdDWORD*- Receives the new thread's id; may be
nullptr.
Return value
Type: ErrorCode
Error::Success (0) on success; otherwise the Win32 error code.
Remarks
The object must not hold a handle yet.
Close
Closes the handle (CloseHandle).
Syntax
ErrorCode Close() noexcept;Return value
Type: ErrorCode
Error::Success (0) on success, also if there is no handle; otherwise the Win32 error code.
Remarks
On failure the object keeps the handle.
Current
CurrentId
Returns the calling thread's id (GetCurrentThreadId).
Syntax
static DWORD CurrentId() noexcept;Return value
Type: DWORD
The id, unique system-wide while the thread exists.
GetExitCode
Gets the exit code (GetExitCodeThread).
Syntax
ErrorCode GetExitCode(DWORD& exitCode) noexcept;Parameters
exitCodeDWORD&- Receives the exit code;
STILL_ACTIVEwhile the thread runs.
Return value
Type: ErrorCode
Error::Success (0) on success; otherwise the Win32 error code.
SetAffinityMask
Sets the affinity mask (SetThreadAffinityMask).
Syntax
ErrorCode SetAffinityMask(DWORD_PTR& affinityMask) noexcept;Parameters
affinityMaskDWORD_PTR&- In: the new mask within the thread's processor group. Out, on success: the previous mask.
Return value
Type: ErrorCode
Error::Success (0) on success; otherwise the Win32 error code.
See also
GetGroupAffinity
Gets processor group and affinity (GetThreadGroupAffinity).
Syntax
ErrorCode GetGroupAffinity(GROUP_AFFINITY& groupAffinity) noexcept;Parameters
groupAffinityGROUP_AFFINITY&- Receives the thread's processor group and affinity mask in it.
Return value
Type: ErrorCode
Error::Success (0) on success; otherwise the Win32 error code.
See also
SetGroupAffinity
Sets processor group and affinity (SetThreadGroupAffinity).
Syntax
ErrorCode SetGroupAffinity(
const GROUP_AFFINITY& groupAffinity,
GROUP_AFFINITY* previousGroupAffinity = nullptr
) noexcept;Parameters
groupAffinityconst GROUP_AFFINITY&- The new processor group and a nonzero affinity mask within it; the reserved fields must be
0. previousGroupAffinityGROUP_AFFINITY*- Receives the previous one; may be
nullptr.Default:nullptr
Return value
Type: ErrorCode
Error::Success (0) on success; otherwise the Win32 error code.
See also
GetId
Gets the thread id (GetThreadId).
Syntax
ErrorCode GetId(DWORD& id) noexcept;Parameters
idDWORD&- Receives the thread's id;
0on failure.
Return value
Type: ErrorCode
Error::Success (0) on success; otherwise the Win32 error code.
GetIdealProcessor
Gets the ideal processor (GetThreadIdealProcessorEx).
Syntax
ErrorCode GetIdealProcessor(PROCESSOR_NUMBER& idealProcessor) noexcept;Parameters
idealProcessorPROCESSOR_NUMBER&- Receives the processor group and number of the ideal processor.
Return value
Type: ErrorCode
Error::Success (0) on success; otherwise the Win32 error code.
See also
SetIdealProcessor
Sets the ideal processor in the thread's group (SetThreadIdealProcessor).
Syntax
ErrorCode SetIdealProcessor(DWORD& idealProcessor) noexcept;Parameters
idealProcessorDWORD&- In: the new processor (
MAXIMUM_PROCESSORSonly queries). Out, on success: the previous one.
Return value
Type: ErrorCode
Error::Success (0) on success; otherwise the Win32 error code.
See also
Sets the ideal processor in any group (SetThreadIdealProcessorEx).
Syntax
ErrorCode SetIdealProcessor(
PROCESSOR_NUMBER& idealProcessor,
PROCESSOR_NUMBER* previousIdealProcessor = nullptr
) noexcept;Parameters
idealProcessorPROCESSOR_NUMBER&- Processor group and number of the new ideal processor.
previousIdealProcessorPROCESSOR_NUMBER*- Receives the previous one; may be
nullptr.Default:nullptr
Return value
Type: ErrorCode
Error::Success (0) on success; otherwise the Win32 error code.
GetIoPendingFlag
Gets whether I/O requests are pending (GetThreadIOPendingFlag).
Syntax
ErrorCode GetIoPendingFlag(BOOL& ioIsPending) noexcept;Parameters
ioIsPendingBOOL&- Receives
TRUEif the thread has I/O requests pending, elseFALSE.
Return value
Type: ErrorCode
Error::Success (0) on success; otherwise the Win32 error code.
GetPriority
Gets the thread priority (GetThreadPriority).
Syntax
ErrorCode GetPriority(int& priority) noexcept;Parameters
priorityint&- Receives the priority, e.g.
THREAD_PRIORITY_NORMAL.
Return value
Type: ErrorCode
Error::Success (0) on success; otherwise the Win32 error code.
See also
SetPriority
Sets the thread priority (SetThreadPriority).
Syntax
ErrorCode SetPriority(int priority) noexcept;Parameters
priorityint- The new priority, e.g.
THREAD_PRIORITY_NORMAL,THREAD_PRIORITY_HIGHESTorTHREAD_MODE_BACKGROUND_BEGIN.
Return value
Type: ErrorCode
Error::Success (0) on success; otherwise the Win32 error code.
See also
GetPriorityBoost
Gets whether priority boosting is disabled (GetThreadPriorityBoost).
Syntax
ErrorCode GetPriorityBoost(BOOL& disablePriorityBoost) noexcept;Parameters
disablePriorityBoostBOOL&- Receives
TRUEif boosting is disabled,FALSEif enabled.
Return value
Type: ErrorCode
Error::Success (0) on success; otherwise the Win32 error code.
See also
SetPriorityBoost
Disables or enables dynamic priority boosting (SetThreadPriorityBoost).
Syntax
ErrorCode SetPriorityBoost(BOOL disablePriorityBoost) noexcept;Parameters
disablePriorityBoostBOOLTRUEdisables boosting,FALSEenables it.
Return value
Type: ErrorCode
Error::Success (0) on success; otherwise the Win32 error code.
See also
GetTimes
Gets the thread's times (GetThreadTimes).
Syntax
ErrorCode GetTimes(
FILETIME& creationTime,
FILETIME& exitTime,
FILETIME& kernelTime,
FILETIME& userTime
) noexcept;Parameters
creationTimeFILETIME&- Receives the creation time (UTC, 100-ns units since 1601-01-01).
exitTimeFILETIME&- Receives the exit time in the same format; undefined while the thread runs.
kernelTimeFILETIME&- Receives the CPU time spent in kernel mode, in 100-ns units.
userTimeFILETIME&- Receives the CPU time spent in user mode, in 100-ns units.
Return value
Type: ErrorCode
Error::Success (0) on success; otherwise the Win32 error code.
Remarks
Creation and exit time, kernel and user CPU time.
See also
QueryCycleTime
Gets the CPU clock cycles the thread has used (QueryThreadCycleTime).
Syntax
ErrorCode QueryCycleTime(ULONG64& cycleTime) noexcept;Parameters
cycleTimeULONG64&- Receives the cycle count, in user and kernel mode together.
Return value
Type: ErrorCode
Error::Success (0) on success; otherwise the Win32 error code.
Suspend
Suspends the OS thread (SuspendThread).
Syntax
ErrorCode Suspend(DWORD& previousSuspendCount) noexcept;Parameters
previousSuspendCountDWORD&- Receives the previous suspend count.
Return value
Type: ErrorCode
Error::Success (0) on success; otherwise the Win32 error code.
Remarks
Not a task suspension; meant for debuggers and tools.
See also
Suspends the OS thread (SuspendThread).
Syntax
ErrorCode Suspend() noexcept;Return value
Type: ErrorCode
Error::Success (0) on success; otherwise the Win32 error code.
Remarks
Discards the previous suspend count.
See also
Resume
Decrements the suspend count (ResumeThread).
Syntax
ErrorCode Resume(DWORD& previousSuspendCount) noexcept;Parameters
previousSuspendCountDWORD&- Receives the previous suspend count.
Return value
Type: ErrorCode
Error::Success (0) on success; otherwise the Win32 error code.
Remarks
The thread runs again when the count reaches zero.
See also
Decrements the suspend count (ResumeThread).
Syntax
ErrorCode Resume() noexcept;Return value
Type: ErrorCode
Error::Success (0) on success; otherwise the Win32 error code.
Remarks
Discards the previous suspend count.
See also
Terminate
Kills the thread (TerminateThread).
Syntax
ErrorCode Terminate(DWORD exitCode) noexcept;Parameters
exitCodeDWORD- The exit code the thread ends with (see
GetExitCode).
Return value
Type: ErrorCode
Error::Success (0) on success; otherwise the Win32 error code.
Remarks
Dangerous: no cleanup, held locks stay held.
Wait
Waits until the thread exits (WaitForSingleObject).
Syntax
ErrorCode Wait(DWORD milliseconds, DWORD& event) noexcept;Parameters
millisecondsDWORD- Timeout in milliseconds;
INFINITEwaits without one. eventDWORD&- Receives the wait result (
WAIT_OBJECT_0,WAIT_TIMEOUT, ...).
Return value
Type: ErrorCode
Error::Success unless the wait failed, so a timeout is Success too.
Remarks
Blocks the whole OS thread: called from a task it blocks the scheduler thread.
QueueUserApc
Queues a user-mode APC to the thread (QueueUserAPC).
Syntax
ErrorCode QueueUserApc(PAPCFUNC apcFunction, ULONG_PTR data) noexcept;Parameters
apcFunctionPAPCFUNC- The function to run on the thread; required.
dataULONG_PTR- Passed unchanged to
apcFunction.
Return value
Type: ErrorCode
Error::Success (0) on success; otherwise the Win32 error code.
Remarks
The APC runs when the thread waits alertably.
Generated from ThinkMeta.ConcurrentTasks.API @ 137ec1d (2026-10-17).