xHarbour Reference Documentation > Function Reference xHarbour Developers Network  

GetThreadID()

Retrieves the numeric application Thread ID of a thread.

Syntax

GetThreadID( [<pThreadHandle>] ) --> nApplicationTID

Arguments

<pThreadHandle>
This the a handle of the thread to retrieve the ID for. If omitted, the thread ID of the current thread is retrieved.

Return

The function returns the numeric ID of a thread as it is used by the xHarbour application.

Description

Function GetThreadID() returns the numeric Thread ID (TID) as it is used by xHarbour's virtual machine. TIDs are unique and are generated sequentially. This guarantees the following conditions:

1. the TID of the Main thread is always 1.

2. the TID of the next thread created is larger than the TID of a previously created thread.

3. if GetThreadID() returns the same TID for two <pThreadHandle>s, both <pThreadHandle>s refer to the same thread.

Note:  if <pThreadHandle> does not contain a valid thread handle, a runtime error is raised. Refer to function IsValidThread() to test the validity of a thread handle.

Info

See also:GetCurrentThread(), GetSystemThreadID(), HB_MutexCreate(), StartThread(), ThreadSleep()
Category: Multi-threading functions , xHarbour extensions
Source:vm\thread.c
LIB:xhbmt.lib
DLL:xhbmtdll.dll

Example

// The example displays the application TID and system TID of
// ten threads.

   PROCEDURE Main
      LOCAL i, aTID := {}

      DisplayTIDs()

      FOR i:=1 TO 9
         StartThread( "GetTIDs", aTID )
      NEXT

      WaitForThreads()

      AEval( aTID, {|c| QOut(c) } )
   RETURN


   PROCEDURE GetTIDs( aTID )
      LOCAL th := GetCurrentThread()
      LOCAL cTID

      IF GetThreadID( th ) == 1
         cTID := "Main thread : "
      ELSE
         cTID := "Child thread: "
      ENDIF

      cTID += "  APP TID:" + Str( GetThreadID( th ), 5 )
      cTID += "  SYS TID:" + Str( GetSystemThreadID( th ), 5 )

      AAdd( aTID, cTID )
   RETURN

Copyright © 2006-2007 xHarbour.com Inc. All rights reserved.
http://www.xHarbour.com
Created by docmaker.exe